Rocket makes a function that says Boarding at gate 4. It works great until a shuttle leaves from gate 7. "Do I need a new function for every gate?" he asks.
Raven looks at the two messages. "What do you notice? The words are the same. Only the number changes."
Nova draws a little slot in the side of a glowing box. "Would you like a hint? A function can take something in through its brackets."
A parameter is a variable in the def line. It waits to receive a value.
The value you hand in when you call the function is called an argument.
def announce(gate):
print("Boarding at gate", gate)
announce(4)
announce(7) | The screen shows |
|---|
| Boarding at gate 4 |
| Boarding at gate 7 |
| Call | gate holds |
|---|---|
| announce(4) | 4 |
| announce(7) | 7 |
Use commas between parameters. The arguments line up with them in the same order.
def fuel_report(ship, units):
print(ship, "has", units, "units")
fuel_report("Shuttle", 40)
fuel_report("Rover", 15) | The screen shows |
|---|
| Shuttle has 40 units |
| Rover has 15 units |
def stars(n):
print("*" * n)
stars(3)
stars(1)
stars(5) def welcome(name, gate):
print("Hi", name, "use gate", gate)
welcome("Kai", 3)
welcome("Ada", 5) Your functions can take values now. Tomorrow they will give answers back.