University of Free Knowledge
QA 76.73 · fol. 10

Naming a Block of Steps

Defining a function stores a named block of steps, and calling that name runs those steps wherever you invoke it. · 10 min

By now your programs are getting longer, and the same few lines keep showing up in more than one place. A function lets you write those lines once, give them a name, and run them again by name alone. There are two separate acts to keep straight: defining a function writes the steps down under a name, and calling it runs those steps. Define once; call as often as you need.

Guess before you learn

Python reads this file top to bottom. Does the word ready ever appear on the screen? `` def setup(): print('ready') print('go') ``

THE DEPTH DIAL — the same idea, younger or deeper
9–12

9–12

Defining a function and calling it are two separate moments. def greet(): binds the name greet to a block of code — the body is stored, not executed. A call, greet(), transfers control into that stored body, runs it to completion, then resumes at the statement after the call. This separation is what lets one definition serve many calls: the steps live in exactly one place, so fixing them once fixes every use. Reusing a name instead of repeating lines is your first real defense against copy-and-paste bugs.

function

A named block of steps. You define it once with def, and you call it — run its steps — by writing its name followed by parentheses.

Why is this true?

Why does reading a def line print nothing, even when the body contains a print?

Because def only stores the block under a name; it does not execute the body. The print inside runs only when the function is called. Defining and calling are separate acts, which is exactly what lets you define a function long before — and far from — where you use it.

latereach new call re-entersdef cheer(): body stored, not runcall: cheer()run the stored bodyreturn to the next line
PLATE I A definition is stored once; each call re-enters the same body and returns.

Trace a program that defines, then calls twice — the steps fade as you master them

1
Python reads def cheer():. Does running the file print anything at this point?
definition stored; body waits
2
The first cheer() runs the stored body top to bottom. How many lines does one call print, given the body is print('go') then print('team')?
'go' then 'team'
3
Control returns, and the second cheer() runs the same body again. How many lines does the second call print?
'go' then 'team' again
4
How many lines are printed in total by the whole program?
2 + 2
Retrieval Gate — answer before you continue 0 / 4

1.Python reads def hello(): with a print inside the body. What happens the moment it reads that line?

2.How many lines does this program print? `` def pair(): print('a') print('b') pair() pair() ``

3.What is the difference between writing greet and writing greet()?

4.Order what happens when a program defines a function and then calls it.

  1. Python stores the function's body under its name
  2. Execution reaches the call line
  3. Control jumps into the stored body and runs it
  4. Control returns to the line after the call

The real payoff of a name is reuse. When the same steps appear in three places, three copies means three places to fix when something changes. Fold them into one function and you fix the steps in one place, and every call sees the fix. Watch the order carefully, though: a call detours into the body, runs it, then comes back to exactly where it left off before continuing down the file.

Ink That Thinks — guess first; the answer draws itself.
Drag these into the order they actually run for the program below. `` def greet(): print('hi') print('start') greet() print('end') ``

  1. def greet(): the block is stored, its body does not run
  2. print('start') runs
  3. greet() is called, so the stored body runs print('hi')
  4. print('end') runs
Reorder, then commit.
PLATE II The runtime order of a define-then-call program — guess in graphite, truth in ink.
MOMENTWHAT YOU WRITEWHAT PYTHON DOESdefinedef cheer():stores the block under the name cheercallcheer()runs the stored block, then returnscall againcheer()runs the same stored block again
PLATE III Two moments, one block: define stores it, each call runs it.
Retrieval Gate — answer before you continue 0 / 4

1.What does this print, in order? `` def mark(): print('-') print('start') mark() print('end') ``

2.The same five lines appear in three places in your program. Why fold them into one function you call three times?

3.In one sentence, explain the difference between defining a function and calling it.

4.How many lines does this print? `` def line3(): print('x') print('y') print('z') line3() ``

So a function is a named block: define it once with def, call it by name whenever you want its steps to run, and control always returns to where the call was made. Right now every call does the exact same thing. Next folio changes that — you will feed values in through parameters and get one value back out with return, so a single definition can do different work each call.

Practice — new ink and old, interleaved

1.How many times does for i in range(2, 7): run its body?

2.Trace this loop. What is the value of i on the line right after the loop ends? `` i = 0 while i < 3: i = i + 1 ``

3.What prints? `` x = 7 if x > 10: print('big') elif x > 5: print('medium') else: print('small') ``

4.How many numbers does range(1, 6) produce?

5.After name = input(), what type is name?

6.Review from folio 2: what is the type of "7", written with quotes?

7.How many times does the body of this loop run? `` for i in range(2, 8): print(i) ``

8.(2 < 3) and (3 < 2) evaluates to?

9.What prints, in order? `` def tag(): print('mid') print('a') tag() print('b') ``

10.How many lines does this print? `` def ping(): print('!') for i in range(3): ping() ``

The Call Slip — search everything Ctrl·K / ⌘K