Lesson 36 of 70 – Python Return Statement
51%

Python Return Statement

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.

Note: When Python executes a return statement, the function immediately stops and the returned value is sent back to the caller.
1. What is the return Statement?

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)
Output:
30
2. Why Use return?

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)
Output:
25
35
3. Basic return Syntax

The basic syntax is:

def function_name():
    return value

Example:

def message():
    return "Hello Python"

print(message())
Output:
Hello Python
4. return Without a Value

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)
Output:
None
5. return Stops Function Execution

When Python executes return, the remaining statements inside that function are not executed.

def test():

    print("Before return")

    return

    print("After return")

test()
Output:
Before return
6. Code After return Is Not Executed
def check():

    return 100

    print("This will not execute")

print(check())
Output:
100
7. Returning a Number
def get_number():
    return 50

number = get_number()

print(number)
Output:
50
8. Returning a String
def get_name():
    return "Amit"

name = get_name()

print(name)
Output:
Amit
9. Returning a Boolean
def is_adult(age):

    if age >= 18:
        return True

    return False

print(is_adult(20))
print(is_adult(15))
Output:
True
False
10. Returning a List

A function can return a list.

def get_fruits():
    return ["Apple", "Banana", "Mango"]

fruits = get_fruits()

print(fruits)
Output:
['Apple', 'Banana', 'Mango']
11. Returning a Tuple
def get_coordinates():
    return 10, 20

coordinates = get_coordinates()

print(coordinates)
Output:
(10, 20)
12. Returning a Dictionary
def get_student():
    return {
        "name": "Rahul",
        "age": 21
    }

student = get_student()

print(student)
Output:
{'name': 'Rahul', 'age': 21}
13. Returning Multiple Values

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)
Output:
(25, 15)
14. Unpacking Multiple Returned Values
def calculate(a, b):

    return a + b, a * b

sum_value, product = calculate(10, 5)

print("Sum:", sum_value)
print("Product:", product)
Output:
Sum: 15
Product: 50
15. return with an Expression

The expression after return is evaluated before the result is returned.

def calculate(a, b):
    return a * b + 10

print(calculate(5, 4))
Output:
30
16. return with Conditional Statements
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))
Output:
Positive
Negative
Zero
17. Multiple return Statements

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))
Output:
Adult
Minor
18. return Inside a Loop

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]))
Output:
Found
19. return vs print()
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)
20. Using a Returned Value in Another Calculation
def square(number):
    return number * number

result = square(5)

final_result = result + 10

print(final_result)
Output:
35
21. Passing a Returned Value to Another Function
def square(number):
    return number * number

def double(number):
    return number * 2

result = double(square(5))

print(result)
Output:
50
22. Returning None

If a function reaches the end without encountering a return statement, it implicitly returns None.

def message():
    print("Hello")

result = message()

print(result)
Output:
Hello
None
23. return in a Real Example
def calculate_percentage(total, obtained):

    percentage = (obtained / total) * 100

    return percentage

percentage = calculate_percentage(500, 425)

print("Percentage:", percentage)
Output:
Percentage: 85.0
24. Returning Student Result
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)
Output:
Total: 300
Average: 75.0
Result: Pass
25. Key Points
  • The return statement sends a value back to the caller.
  • return immediately stops the current function execution.
  • A function can return numbers, strings, lists, tuples, dictionaries, and other objects.
  • A function can return multiple values.
  • Multiple returned values are commonly received through tuple unpacking.
  • A function without an explicit return statement returns None.
  • return and print() have different purposes.
  • return can be used with conditions and loops.
  • A returned value can be stored in a variable and used in another calculation.

🧠 Quick Quiz

Question: What does the return statement do in a Python function?