10
Lesson 10
Using return to get function results
In Python, the return statement is used in functions to send back a value (or values) to the caller. When a function is called, it may perform computations or operations and then use the return statement to provide the result of those operations.
This allows for more modular and reusable code since functions can produce outputs that can be used elsewhere in the program.
1. Purpose of the return statement:
This allows for more modular and reusable code since functions can produce outputs that can be used elsewhere in the program.
1. Purpose of the return statement:
The primary purpose of return is to allow a function to output a value. This value can then be used for further processing or stored in a variable.
When a return statement is executed, it immediately terminates the function’s execution and returns control back to the point where the function was called. If there is any code after the return statement within the function, it will not be executed.
Here’s a simple example to illustrate how the return statement works:
def add(a, b): result = a + b # Calculate the sum return result # Return the result to the caller sum_value = add(3, 5) # Calling the function and storing the result print("The sum is:", sum_value) # Output: The sum is: 8
Explanation
- The function add(a, b) takes two parameters and calculates their sum.
- The result is returned to the caller when the function is executed.
- The returned value is stored in the variable sum_value, which is then printed.
2. Multiple return values:
A function can also return multiple values as a tuple. This can be useful when you want to return more than one piece of information.
def arithmetic_operations(x, y): addition = x + y subtraction = x - y return addition, subtraction # Returning both values as a tuple result_sum, result_difference = arithmetic_operations(10, 4) # Unpacking the returned tuple print("Sum:", result_sum) # Output: Sum: 14 print("Difference:", result_difference) # Output: Difference: 6
Explanation:
- In the arithmetic_operations(x, y) function, both the sum and the difference of x and y are calculated.
- These two values are returned as a tuple, which can be unpacked into separate variables (result_sum and result_difference).
3. Implicit Return:
If no return statement is provided in a function, Python implicitly returns None.
def no_return(): print("This function has no return statement.") result = no_return() # Calling the function print(result) # Output: None
Explanation:
- The function no_return() performs a print operation but does not include a return
statement. - When called, it returns None`, which is stored in the variable result.
Summary
- The return statement is essential for producing outputs from functions in Python.
- It allows functions to send back calculated results to the caller, facilitating code reuse and modular design.
- Functions can return a single value, multiple values as a tuple, or implicitly return None if no return is specified.
- Understanding how to use return effectively is fundamental for writing functional and organized Python code.