return hands a value back to whoever called the function, and immediately ends the function. Understanding return — including how to give back several values and what happens when you omit it — is essential to composing functions together.
Returning a value
def double(n):return n * 2print("never runs") # unreachable — return already exitedresult = double(21) # 42 — the returned value
Returning multiple values
Return several values as a tuple (the commas make it one), then unpack them at the call site:
def stats(xs):return min(xs), max(xs), sum(xs) / len(xs)lo, hi, avg = stats([4, 8, 6]) # lo=4, hi=8, avg=6.0
Returning None
A function with no return (or a bare return) gives back None. Use a bare return for an early exit:
def log(msg):print(msg) # no return → returns Nonedef withdraw(balance, amount):if amount > balance:return # early exit, returns Nonereturn balance - amount
Key takeaways
- 01return sends a value back and immediately ends the function.
- 02Return a tuple (comma-separated) to give back multiple values; unpack at the call site.
- 03A function without return (or a bare return) yields None.
- 04A bare return is a clean early exit.
- 05Prefer returning values over printing inside functions — it keeps them testable.
