Return Values
Ohhhh YES β Step 3 is here, and this is where functions stop being cute little βdo-a-thingβ buttons and start becoming magical vending machines that GIVE YOU STUFF BACK.
This is the moment your code goes from:
βLook, I printed something!β
to
βHere, I calculated something important for you. Youβre welcome.β
Letβs go.
Alright coder, up until now your functions have been like that one friend who talks a lot but never actually gives you anything.
But now?
Now your functions can return things.
- Numbers
- Strings
- Results
- Calculations
Basically: loot drops.
A return value is your function saying:
βHereβs the thing you asked for. Go use it.β
1. What Does return Do?
return sends a value back to wherever the function was called.
Itβs like:
- ordering food β getting food
- casting a spell β getting damage numbers
- asking your friend for notes β getting notes
Without return, your function just prints stuff and leaves.
With return, your function becomes USEFUL.
2. Basic Example
def add(a, b):
return a + b
Now you can do:
def add(a, b):
return a + b
result = add(5, 7)
print(result)
Output:
12
Your function just did math FOR you.
You didnβt even break a sweat.
3. Game Example: Damage Calculator
def calculate_damage(base, bonus):
return base + bonus
Use it like:
hit = calculate_damage(10, 5)
print("You deal", hit, "damage!")
Your game now calculates damage like a real RPG.
4. Functions Can Return ANYTHING
Numbers? Yes.
Strings? Yes.
Lists? Absolutely.
A sense of purpose? β¦weβre working on it.
Example:
def get_inventory():
return ["sword", "potion", "map"]
Boom. Your function just handed you an entire backpack.
Practice (Your Turn!)
Mini-Challenge
Create a function called double(x) that returns x times 2.
Then print the result of:
double(6)
Your function now multiplies things like a tiny math wizard.


