Lesson 9 of 26 · about 15 min
Functions: your own instructions
def creates a new instruction with a name. Parameters are its inputs, and return sends a result back to whoever called it.
🧭 What you need first: Meet Bit the robot
Think about a blender. You don’t need to know how the motor works. You put things in (banana, milk), press the button, and get something out (a smoothie). Different ingredients in, different smoothie out, same blender.
A function is a blender you build yourself. You write the recipe once, give it a name, and from then on you just “press the button” by calling its name. The inputs are called parameters, and what comes out is the return value.
You’ve already used functions someone else built: print(), input(), len(), move(). Now you’ll make your own.
Another way: a vending machine
Put in a code (input), get a snack (output). The machine does the same steps every time; only the input changes. You don’t care what happens inside, only what goes in and what comes out.
Another way: a recipe card titled “Pancakes”
Once a recipe card exists, you can just say “make pancakes” instead of listing every step. “Make pancakes for 4” changes one input. A function is a recipe card your program can refer to by name.
Watch a function get called
Step through. Notice that def doesn’t run the body; it just creates the function. The body only runs when line 5 calls it, and a new box appears for that call.
Watch the code run, one step at a time
- 1def area(width, height):
- 2 result = width * height
- 3 return result
- 4
- 5kitchen = area(3, 4)
- 6hallway = area(1, 6)
- 7print(kitchen + hallway)
➜ yellow = the line about to run · ✓ = the line that just ran
The real words
def area(width, height): # header: name and parameters
"""Return the area of a rectangle.""" # docstring
return width * height # bodydefdefines a function. Defining it doesn’t run it.widthandheightare parameters: placeholder names for inputs.area(3, 4)calls the function.3and4are the arguments, the actual values. Python matches them to the parameters in order.- return sends a value back to wherever the function was called and stops the function immediately.
- No
return? The function gives back the special valueNone(“nothing”). - The text in triple quotes is a docstring: a short description of what the function does. It’s for humans, and tools show it as help.
print vs return, the classic confusion:
print(x) |
return x |
|
|---|---|---|
| What it does | shows text on screen | hands a value back to the code |
| Can you use the result? | no, it’s just displayed | yes: store it, add it, compare it |
Why functions? Write once, use many times (no copy-pasting). Give a name to an idea so code reads like English. Fix a bug in one place.
Write two functions
Write to_fahrenheit(celsius) that returns the temperature in Fahrenheit (multiply by 9, divide by 5, add 32). Then write is_even(n) that returns True if n is even, False if not.
print or return?
def double(x): print(x * 2) …then: y = double(5) and print(y + 1). What happens?
Write longest(a, b) that returns whichever string is longer (if they're the same length, return a).
In def greet(name): … greet("Sam"), which is the parameter and which is the argument?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?