Python return statement
The return statement is one of the most fundamental concepts in Python (and in other programming languages) when you are working with functions. This is how you can send a value back from a function to the place where it was called. Let’s break this down step by step.
1. What is a return Statement?
The return statement is used in a function to:
- Immediate exit from the function.
- Optionally return a value (or values) to the location where the function was invoked.
Syntax:
def function_name():
return value
2. How Does the return Statement Work?
a) Exiting the Function
If there is a return statement, the function stops executing right at that point. None of the programming written after the return statement in that same function would run.
Example:
def say_hello():
print("Hello")
return
print("This will not print") # This line will not be executed.
say_hello()
Output:
Hello
b) Returning a Value
You can use return to send a value from the function to where it was called.
Example:
def add_numbers(a, b):
return a + b
result = add_numbers(5, 10)
print(result)
Output:
15
3. Returning Multiple Values
In Python, you can return multiple values from a function by separating them with commas. They are returned as a tuple.
Example:
def get_coordinates():
x = 5
y = 10
return x, y
coords = get_coordinates()
print(coords) # Entire tuple
print(coords[0]) # First value (x)
print(coords[1]) # Second value (y)
Output:
(5, 10)
5
10
4. Using the Returned Value
The value returned from a function can be used in further calculations or logic.
Example:
def multiply(a, b):
return a * b
result = multiply(3, 4)
print(result * 2) # Using the returned value in a calculation
Output:
24
5. return vs. print
printis used to display something to the console.returnsends data back from the function and allows further use.
Example:
def add(a, b):
print(a + b) # Displays the result but doesn't return anything
return a + b # Returns the result for further use
result = add(5, 3)
print("Returned value:", result)
Output:
8
Returned value: 8
6. Default Return Value
If a function doesn’t have a return statement, it returns None by default.
Example:
def no_return():
x = 5 # No `return`
result = no_return()
print(result)
Output:
None
7. Returning Conditional Results
You can use return with conditional logic to send specific results based on the conditions.
Example:
def check_even(number):
if number % 2 == 0:
return True
return False
print(check_even(4)) # True, because 4 is even
print(check_even(7)) # False, because 7 is odd
Output:
True
False
8. Returning Complex Data Structures
You can return more complex data types, like lists or dictionaries.
a) Returning a List
def generate_list():
return [1, 2, 3, 4]
print(generate_list())
Output:
[1, 2, 3, 4]
b) Returning a Dictionary
def user_info():
return {"name": "Alice", "age": 30}
info = user_info()
print(info["name"]) # Accessing a value from the dictionary
Output:
Alice
9. Early Return
The return statement can be used to exit a function early if a specific condition is met.
Example:
def find_divisible(number):
if number % 5 == 0:
return "Divisible by 5"
return "Not divisible by 5"
print(find_divisible(10)) # Exits early with "Divisible by 5"
print(find_divisible(7)) # Exits with "Not divisible by 5"
Output:
Divisible by 5
Not divisible by 5
10. Chaining Returns
You can chain multiple functions where one function’s return value is used as the input for another.
Example:
def double(x):
return x * 2
def triple(x):
return x * 3
result = double(triple(4)) # triple(4) returns 12, double(12) returns 24
print(result)
Output:
24
Summary
- A
returnstatement is a way to return a value (or values) from a function back to the caller. - If no
returnvalue is specified, it returnsNone. returnis different fromprint, which only displays a value.- You can return simple values, multiple values, or even complex data structures like lists, dictionaries, or functions.
- It enables to handle early exit or chaining logic.