Return Values
Getting a result back from a function using return — the difference between a function that does something and one that produces something.
"print() shows a value on screen. return sends the value back to whoever called the function — so they can use it, store it, or pass it somewhere else."
— ShurAIprint vs return — The Key Difference
This is the most important concept in this lesson. Both look similar but they do very different things:
The function gives nothing back.
Cannot store the result.
Like a chef who cooks AND eats the food.
The caller decides what to do with it.
Can be stored, calculated, passed on.
Like a chef who cooks and hands it to you.
# Version 1 — uses print (result is LOST)
def add_print(a, b):
print(a + b) # shown on screen, gone
result = add_print(3, 4) # prints 7
print(result) # None — nothing was returned!
# Version 2 — uses return (result is USABLE)
def add_return(a, b):
return a + b
result = add_return(3, 4) # result = 7
print(result) # 7
print(result * 2) # 14 — can use it!
How return Works
When Python hits a return statement, it immediately exits the function and sends the value back to where the function was called:
def square(n):
return n * n
print("This line NEVER runs!") # after return = unreachable
x = square(5) # x = 25
y = square(3) # y = 9
print(x + y) # 34
# Returned value used directly in an expression
print(square(4) + square(3)) # 25
Returning Multiple Values
Python functions can return multiple values at once as a tuple. Unpack them directly into variables:
def min_max(numbers):
"""Return both the smallest and largest number."""
return min(numbers), max(numbers)
scores = [72, 45, 91, 66, 88]
# Unpack the two returned values
lowest, highest = min_max(scores)
print(f"Lowest : {lowest}") # 45
print(f"Highest: {highest}") # 91
Early Return — Exit When Done
return can be used to exit a function early, just like break exits a loop. Great for guard clauses:
def divide(a, b):
if b == 0:
return "Error: cannot divide by zero" # early exit
return a / b
print(divide(10, 2)) # 5.0
print(divide(10, 0)) # Error: cannot divide by zero
Real Example — Shopping Discount Calculator
def get_discount(total):
"""Return the discount percentage based on cart total."""
if total >= 2000:
return 20
elif total >= 1000:
return 10
elif total >= 500:
return 5
else:
return 0
def checkout(name, cart_total):
"""Print the full checkout summary."""
discount_pct = get_discount(cart_total) # calling another function
discount_amt = cart_total * discount_pct / 100
final = cart_total - discount_amt
print(f"\n{'='*35}")
print(f" Customer : {name}")
print(f" Cart : Rs.{cart_total}")
print(f" Discount : {discount_pct}% (-Rs.{discount_amt:.0f})")
print(f" YOU PAY : Rs.{final:.0f}")
print(f"{'='*35}")
return final
checkout("Riya", 350)
checkout("Arjun", 1250)
checkout("Sneha", 2400)
===================================
Customer : Riya
Cart : Rs.350
Discount : 0% (-Rs.0)
YOU PAY : Rs.350
===================================
===================================
Customer : Arjun
Cart : Rs.1250
Discount : 10% (-Rs.125)
YOU PAY : Rs.1125
===================================
===================================
Customer : Sneha
Cart : Rs.2400
Discount : 20% (-Rs.480)
YOU PAY : Rs.1920
===================================
"A function that only prints is a dead end. A function that returns gives you a result you can use, chain, and build on."
— ShurAI🧠 Quiz — Q1
What does return do in a function?
🧠 Quiz — Q2
What does a function return if there is no return statement?
🧠 Quiz — Q3
What happens to code written after a return statement?
🧠 Quiz — Q4
Given def add(a,b): return a+b — what does result = add(3, 4) store in result?