← Back to course
Code Crew G6+ / Week 06 / Tuesday
2/6
Week 06 Β· Functions and modules

Tuesday

Parameters
// Name a job once, use it everywhere
⏱ about 30 min

Tuesday: Parameters

Rocket's new status function always prints the same thing: Station 1, online. But Mission Control has twelve stations, and some of them are offline.

"I could write twelve functions," he says, then shakes his head. "No. That is the copy problem all over again."

Raven studies the line inside the function. "What do you notice?" she asks. "Only two parts change: the station number and the status. Everything else stays the same."

Nova draws two empty slots in the function's brackets. "I found a pattern," she says. "Give the function slots for the parts that change. Each call fills them in."

Rocket adds two names between the brackets.

Parameters and arguments

A parameter is a name in the brackets of def. An argument is the value you put in the brackets when you call. When the function runs, each parameter holds its argument.

Arguments match parameters by position: the first argument goes to the first parameter, the second to the second.

# station_status.py
def show_status(station, status):
    print(f"Station {station}: {status}")

show_status(1, "online")
show_status(2, "offline")
show_status(3, "online")
Station 1: online
Station 2: offline
Station 3: online
In show_status(2, "offline"), what value does station hold?
What does show_status(9, "sleeping") print? Station 9: ____

Parameters work with loops

# all_stations.py
def show_status(station, status):
    print(f"Station {station}: {status}")

offline = [2, 5]
for number in range(1, 6):
    if number in offline:
        show_status(number, "offline")
    else:
        show_status(number, "online")
Station 1: online
Station 2: offline
Station 3: online
Station 4: online
Station 5: offline
ARGUMENTS IN ORDER
  • Read the question.
  • Tap your answer.
def move(x, y): prints x then y. What does move(4, 7) print first?
You call show_status(1) with only one argument. What happens?
At your computer
1. Type station_status.py, save it and run it.
2. Add a call for station 4 that shows "repairing".
3. Type all_stations.py and run it. Add station 4 to the offline list and run again.
4. Call show_status(1) with one argument. Read the TypeError, then fix the call.

Practice: a loop inside a function

A function body can hold a loop. A parameter can control how many times it goes round.

# countdown_fn.py
def countdown(start):
    for n in range(start, 0, -1):
        print(n)
    print("Go!")

countdown(3)
countdown(2)
3
2
1
Go!
2
1
Go!
TRACE COUNTDOWN_FN.PY
  • Read the question.
  • Tap your answer.
What does countdown(3) show first?
Why does Go! appear only once per call?
How many lines does countdown(5) show?
What does countdown(1) show first?
What is the only line countdown(0) shows?
def show(a, b): print(b, a). What does show(1, 2) print?
StatementTrue or false?
A parameter is a name in the def line.?
An argument is the value given in a call.?
Arguments fill parameters from right to left.?
A function can have a loop inside it.?
WHY THIS EXERCISEParameters are the names, arguments are the values, and they match up left to right.

Parameters make one function useful in many situations. Tomorrow you will learn the difference between printing a result and returning it.

← Monday