Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 05

Python return Statement — Explicit, Implicit, and Multiple Return Values

Learn how return exits a Python function, sends values back to the caller, returns None implicitly, and packs multiple values into tuples.

Intermediate30 min

What you will learn

  • What return does: stop the function and send a value back
  • Explicit return, implicit None, and bare return
  • The early return pattern
  • Returning any object, including multiple values
  • The difference between print() and return

What is return?

A return statement does two jobs at the same time:

  1. Stops the function immediately
  2. Sends a value back to the caller

This continues from Local and Global Variables.

Caller add(10,20) into function a+b return 30 then result 30
python
def add(a, b):
    return a + b

result = add(10, 20)
print(result)

Output:

code
30

Explicit return

An explicit return means you deliberately send a value back.

python
def return_42():
    return 42

x = return_42()
print(x)
return 42 travels from the function to x = 42 in the caller

Returned values are reusable anywhere an expression is valid:

python
print(return_42())        # 42
print(return_42() * 2)    # 84
print(return_42() + 8)    # 50

return is only valid inside a def (or lambda). Outside a function it is a SyntaxError.


return immediately stops execution

This is one of the most important rules.

print Start then return 6 then print Never Runs labeled dead code
python
def my_function():
    print("Start")
    return 6
    print("End")     # never executes

print(my_function())

Output:

code
Start
6

Everything after return on the same path becomes dead code.


Early return pattern

Professional developers use return to exit early when input is invalid.

python
def safe_divide(a, b):
    if b == 0:
        return "Cannot divide"
    return a / b

print(safe_divide(10, 2))
print(safe_divide(10, 0))
Decision: if b == 0 return error else return a/b then function ends

Output:

code
5.0
Cannot divide

Implicit return (None)

If you forget return, Python automatically returns None.

python
def add_one(x):
    y = x + 1

r = add_one(5)
print(r)

Output:

code
None
No return statement then Python returns None

Bare return

A return can exist without a value. It still exits immediately and sends None.

python
def greet(name):
    if not name:
        return
    print("Hello", name)

greet("Alice")
print(greet(""))
Input Output
"Alice" Hello Alice
"" Returns None

Return any Python object

Everything in Python is an object — so everything can be returned.

Six cards: integer, string, list, dictionary, tuple, function
python
def student():
    return {"id": 101, "name": "Alice"}

info = student()
print(info["name"])

Output:

code
Alice

You can also return a function:

python
def multiplier(factor):
    return lambda x: x * factor

double = multiplier(2)
print(double(5))    # 10

Returning multiple values

Python does not really return multiple values — it returns one tuple.

Function return a, b, c becomes tuple (10, 20, 30)
python
def stats():
    return 10, 20, 30

x, y, z = stats()
print(x)
print(z)

Output:

code
10
30

Python packs on return and unpacks on assignment.

Packing return 1,2,3 into a tuple; unpacking into a, b, c
Packing Unpacking
Idea Many values become one tuple Tuple becomes many variables
Code return 1, 2, 3 a, b, c = stats()
Result (1, 2, 3) a=1 b=2 c=3

This is the #1 beginner mistake.

print displays and caller gets None; return delivers a reusable value
python
def double(x):
    print(x * 2)

print(double(5))

Output:

code
10
None

print() shows a value on screen. It does not send it back. Using return:

python
def double(x):
    return x * 2

print(double(5))    # 10 — and the caller can reuse it

Memory rule: Print displays. Return delivers.


Real project example

python
def calculate_price(base, tax):
    total = base + tax
    return total

price = calculate_price(500000, 25000)
print(price)

Why return instead of print? Another function may need the value:

python
invoice = calculate_price(500000, 25000)
# save_to_database(invoice)
print("Invoice total:", invoice)

Reusable code always favors return.


Common mistakes

Mistake Problem Fix
Forget return Returns None return value
Code after return Never executes Move it above
return outside a function SyntaxError Use inside def
print(print()) Inner print returns None Return the value instead

Classic trap:

python
def compute(x):
    print(x * 2)

value = compute(5)
print(value)

Output:

code
10
None

Correct:

python
def compute(x):
    return x * 2

value = compute(5)
print(value)    # 10

Interview cheat sheet

Question Answer
What does return do? Exit + send a value
No return statement? Returns None
Bare return? Returns None
Code after return? Dead code
Can you return a list or dict? Yes
Can you return a function? Yes
Multiple values? One tuple

Mini challenge

Predict the output without running it:

python
def demo(x):
    if x > 5:
        return x * 2
    return

a = demo(10)
b = demo(3)
print(a)
print(b)
  • demo(10) → returns 20
  • demo(3) → hits bare return → None

Output:

code
20
None

If you understand explicit return, implicit None, early exit, and tuple return, you have mastered return.


Practice in the playground

The playground at the bottom already runs explicit return, early exit, None, packing, and print vs return. Try:

  1. Change return_42() to return 7 and confirm * 2 prints 14.
  2. Swap safe_divide(10, 0) to safe_divide(9, 3) — expect 3.0.
  3. Replace return in double_return with print and watch None appear.

Next: Lambda Functions.

Loading playground…

Check your understanding

Return statement quiz

What does the `return` keyword do in a function?

1 / 10