
Return Statement
In Function Definition and Parameters, we covered function creation and parameter usage. While we've utilized the print()
method to display function results in the console, practical scenarios often require using return
instead of print
. Unlike print
, return
allows the function result to be stored in a variable for further use.
In Python, the return
statement is used to exit the function and return a value to the caller. This allows functions to compute a value and provide it for further use outside of the function.
Let's explore how the return
statement work in Python functions.
Return
Statement: return a value to the caller
In the example below, a function named add
takes two arguments a
and b
, adds them together, and returns the result using the return
statement.
# Create a functiondef add(a, b): # Use return statement to return the sum of a and b return a + b
Then call the add()
function with positional arguments 3 and 5, the returned result was stored in the variable result
. Subsequently, we can access the function result through the result
variable.
# Store function result in the result variableresult = add(3, 5)print(result)
Output:
8
Recall from Function Definition and Parameters, print()
function is used within the add
function. Let's explore how this approach differs from using a return statement.
# Create a functiondef add_print(a, b): # Use print() method inside the function print(a + b)
# Call the add_print() function and # store function result to the result_print variableresult_print = add_print(3, 5)
# Print the result_print variable returns "None"print(result_print)
Output:
8
None
In this example, the add function takes two parameters a
and b
, and it prints the sum of a
and b
using the print statement. However, there is no return
statement in the function, which means the function doesn't explicitly return any value.
When the function is invoked with add_print(3, 5)
, it prints the result of 3 + 5
(which is 8) to the console. However, since there is no return
value, the variable result_print
will be assigned None
. Therefore, when print(result_print)
is executed, it prints None
, indicating that no value was returned from the function.
Return
Statement: exit a function
The return
statement in Python functions marks the exit point for the function, irrespective of its location within the function. Once executed, the return statement concludes the function's execution, halting any further code execution that follows it.
Example
The function counter()
below initializes a variable count
to 0. It then enters a while
loop that continues until the count
value is larger than 10. Within the loop, there's a condition that checks if count
is equal to 5. If it is, the function immediately exits and returns the value of count
. Without the return
statement, it increments count
value by 1 and continues looping until count
reaches or exceeds 10.
# Create function counter()def counter(): # Initialize a variable count with value 0 count = 0
# While loop while count <=10: # If condition statement to check if the count value equals 5 if count == 5: # Return the count value and exit the function if count euqal 5 return count # Increment count by 1 count += 1
Call the counter()
function:
# Call the counter() function and store function result to a variableresult = counter()print(result)
Output:
5
Because the if
condition checks if count
equals 5, once this condition is met, the return
statement executes to exit the function, returning the current value of count. Consequently, the count
will never be incremented beyond 5.
Example
The code below defines a function named example_function()
. Within this function, an empty list init_list
and a variable a
are initialized. Then, a while
loop runs as long as the variable a
is less than 10.
During each iteration, the value of a
is added to init_list
, and a
is incremented. If the length of init_list
becomes 3, the function exits prematurely, returning the current state of init_list
. Otherwise, the loop continues until a
is larger than or equal to 10.
# Create a functiondef example_function(): # Initialize an empty list init_list = [] # Initialize a variable with 5 a = 5
# While loop while a < 10: # Append current value of a to the init_list list init_list.append(a) # Increment a by 1 a += 1 # If condition to check the length of the init_list if len(init_list) == 3: # If the init_list contains three elements # return the list and exit the function return init_list
Call the example_function()
function:
result = example_function()print(result)
Output:
[5, 6, 7]
Similar to the previous example, the if
condition and return
statement ensures that init_list
never contains more than three values. The return
statement causes the function to terminate prematurely once init_list
reaches a length of three.