Return Values

STEP 3 β€” Return Values (a.k.a. Functions That Give You Loot)

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)
Click Run

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)
Modify and Run

Your function now multiplies things like a tiny math wizard.