Python return statement
A return statement is used to end the execution of the function call and it "returns" the value of the expression following the return keyword to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to invoke a function so that the passed statements can be executed.
Example:
def add(a, b):
# returning sum of a and b
return a + b
def is_true(a):
# returning boolean of a
return bool(a)
# calling function
res = add(2, 3)
print(res)
res = is_true(2<5)
print(res)
Output
5 True
Explanation:
- add(a, b) Function: Takes two arguments a and b. Returns the sum of a and b.
- is_true(a) Function: Takes one argument a. Returns the boolean value of a.
- Function Calls: res = add(2, 3) computes the sum of 2 and 3, storing the result (5) in res. res = is_true(2 < 5) evaluates the expression 2 < 5 (which is True) and stores the boolean value True in res.
Let's explore python return statement in detail:
Table of Content
Syntax:
def function_name(parameters):
# Function body
return value
When the return statement is executed, the function terminates and the specified value is returned to the caller. If no value is specified, the function returns None by default.
Note:
Note: Return statement can not be used outside the function.
Returning Multiple Values
Python allows you to return multiple values from a function by returning them as a tuple:
Example:
def fun():
name = "Alice"
age = 30
return name, age
name, age = fun()
print(name)
print(age) # Output: 30
Output
Alice 30
In this example, the fun() function returns two values: name and age. The caller unpacks these values into separate variables.
Returning List
We can also return more complex data structures such as lists or dictionaries from a function:
def fun(n):
return [n**2, n**3]
res = fun(3)
print(res)
Output
[9, 27]
In this case, the function fun() returns a list containing the square and cube of the input number.
Function returning another function
In Python, functions are first-class citizens, meaning you can return a function from another function. This is useful for creating higher-order functions.
Here's an example of a function that returns another function:
def fun1(msg):
def fun2():
# Using the outer function's message
return f"Message: {msg}"
return fun2
# Getting the inner function
fun3 = fun1("Hello, World!")
# Calling the inner function
print(fun3())
Output
Message: Hello, World!
What is the primary purpose of a return statement in a function?
-
A
To initiate a function call
-
B
To terminate the program
-
C
To end the execution of the function and return a value
-
D
To define the function parameters
-
AThe function continues executing
-
BThe function raises an error
-
CThe function returns a default value of None
-
DThe function returns the last executed statement
-
ABy using multiple return statements
-
BBy returning a list or tuple
-
CBy returning a dictionary only
-
DBy using global variables
What will be the output of the following code?
def my_function():
return 5
result = my_function()
print(result)
-
A
None
-
B
5
-
C
Error
-
D
my_function
return statement returns the value 5 from the function my_function(). The returned value is assigned to result and printed.
-
AThe unreachable code will execute
-
BThe function will raise an exception
-
CThe unreachable code will be ignored
-
DThe function will return an error
What will be the output of the following code?
def check_number(num):
if num > 0:
return "Positive"
else:
return "Non-positive"
print(check_number(5))
-
A
Non-positive
-
B
Positive
-
C
None
-
D
Error
check_number function checks if the input is positive. Since the argument is 5, it returns "Positive".
What will be the output of the following code?
def my_function():
return 1
return 2
result = my_function()
print(result)
-
A
1
-
B
2
-
C
None
-
D
Error
In the my_function function, the first return 1 statement terminates the function, so the second return 2 is never reached. Thus, the returned value is 1.
