The return statement is used inside a function to send a result back to the code that called the function. It can return numbers, strings, lists, tuples, dictionaries, objects, or other values.
The return statement sends a value from a function back to the calling code.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
The return statement is useful when the result of a function needs to be used somewhere else in the program.
def square(number):
return number * number
result = square(5)
print(result)
print(result + 10)
The basic syntax is:
def function_name():
return value
Example:
def message():
return "Hello Python"
print(message())
A function can use return without specifying a value. In that case, the function returns None.
def stop_program():
return
result = stop_program()
print(result)
When Python executes return, the remaining statements inside that function are not executed.
def test():
print("Before return")
return
print("After return")
test()
def check():
return 100
print("This will not execute")
print(check())
def get_number():
return 50
number = get_number()
print(number)
def get_name():
return "Amit"
name = get_name()
print(name)
def is_adult(age):
if age >= 18:
return True
return False
print(is_adult(20))
print(is_adult(15))
A function can return a list.
def get_fruits():
return ["Apple", "Banana", "Mango"]
fruits = get_fruits()
print(fruits)
def get_coordinates():
return 10, 20
coordinates = get_coordinates()
print(coordinates)
def get_student():
return {
"name": "Rahul",
"age": 21
}
student = get_student()
print(student)
Python allows a function to return multiple values. These values are returned together as a tuple.
def calculate(a, b):
return a + b, a - b
result = calculate(20, 5)
print(result)
def calculate(a, b):
return a + b, a * b
sum_value, product = calculate(10, 5)
print("Sum:", sum_value)
print("Product:", product)
The expression after return is evaluated before the result is returned.
def calculate(a, b):
return a * b + 10
print(calculate(5, 4))
def check_number(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
return "Zero"
print(check_number(10))
print(check_number(-5))
print(check_number(0))
A function can contain multiple return statements. Only the first return statement reached during execution returns a value and ends that function call.
def check_age(age):
if age >= 18:
return "Adult"
return "Minor"
print(check_age(20))
print(check_age(15))
A return statement inside a loop immediately ends the entire function.
def find_number(numbers):
for number in numbers:
if number == 30:
return "Found"
return "Not Found"
print(find_number([10, 20, 30, 40]))
| return | print() |
|---|---|
| Sends a value back to the caller | Displays a value on the screen |
| Can be stored in a variable | Returns None |
| Ends the current function execution | Does not end a function by itself |
def add(a, b):
return a + b
result = add(10, 20)
print(result)
def square(number):
return number * number
result = square(5)
final_result = result + 10
print(final_result)
def square(number):
return number * number
def double(number):
return number * 2
result = double(square(5))
print(result)
If a function reaches the end without encountering a return statement, it implicitly returns None.
def message():
print("Hello")
result = message()
print(result)
def calculate_percentage(total, obtained):
percentage = (obtained / total) * 100
return percentage
percentage = calculate_percentage(500, 425)
print("Percentage:", percentage)
def get_result(marks):
total = sum(marks)
average = total / len(marks)
if average >= 40:
result = "Pass"
else:
result = "Fail"
return total, average, result
marks = [80, 70, 90, 60]
total, average, result = get_result(marks)
print("Total:", total)
print("Average:", average)
print("Result:", result)
Question: What does the return statement do in a Python function?