The return statement is used inside a function to send a result back to the place where the function was called.
When Python reaches a return statement, the function stops executing and sends the value back.
WHY USE RETURN?
• To get output from a function
• To reuse calculated results
• To make functions more useful and flexible
BASIC SYNTAX
def function_name():
return value
EXAMPLE 1: SIMPLE RETURN
def add(a, b):
return a + bresult = add(5, 3)
print(result)
The function calculates the sum and returns 8.
WITHOUT RETURN VS WITH RETURN
Without Return
def add(a, b):
print(a + b)result = add(5, 3)
print(result)
Output:
8
None
Because the function does not return anything, Python automatically returns None.
With Return
def add(a, b):
return a + bresult = add(5, 3)
print(result)
Output:
8
RETURNING MULTIPLE VALUES
A function can return multiple values separated by commas.
def calculate(a, b):
sum_value = a + b
diff_value = a - b
return sum_value, diff_valueresult = calculate(10, 5)
print(result)
Python returns them as a tuple.
You can also unpack them:
sum_result, diff_result = calculate(10, 5)
print(sum_result)
print(diff_result)
RETURNING BOOLEAN VALUES
def is_even(num):
return num % 2 == 0print(is_even(4))
This function returns True if the number is even.
IMPORTANT POINTS
• A function stops immediately after return
• Code written after return inside the same block will not execute
• If no return is written, Python returns None by default
Example:
def test():
return "Hello"
print("This will not run")
WHY RETURN IS IMPORTANT
• Allows functions to produce reusable output
• Helps in performing calculations and storing results
• Essential for building structured and professional programs
Understanding the return statement is key to writing efficient and functional Python programs.