Give a repeated task a name
Leans on
Quick review (2 minutes)
A recipe card titled Tea sits in a kitchen drawer.
A def line and its indented body define a function without running the body. The functions of this lesson have no parameters. Once such a function has been defined, a call, written as its name followed by an empty pair of parentheses, runs the body from its first line. When the body finishes, the program carries on from the place of the call. Each such call runs the body once.
Find the lines that the program needs in more than one place.
Write
def, a name that says what the lines do, an empty pair of parentheses and a colon.Put the lines under the
defline and indent them one more level, four spaces, keeping any deeper indentation they already have.Leave the indented block, and write the function's name with parentheses at each place where the lines should run.
Trace the program: skip the body at the
defline, and run the body at each call.
The parentheses make the call. On a line below the function's definition, the name alone, show_banner with no parentheses, does not call it. The file displays no banner and reports no error. Check for the parentheses when a function seems to do nothing.
The body belongs to the function only while it is indented. A print line at the margin, after the body, is not part of the function. It runs once, when Python reaches it, and not at each call.
1 In plain words
A recipe card titled Tea sits in a kitchen drawer. Writing the card makes no tea. When someone says to make tea, the cook takes out the card and follows its steps, and the card is there again the next time.
A function is a block of lines with a name. A def line starts it: the word def, the function's name, a pair of parentheses and a colon. The indented lines under it are the function's body.
Defining a function does not run its body. The body runs when the defined function is called, and it runs again at every such call. In this lesson a call to one of your own functions is its name followed by an empty pair of parentheses, such as show_banner().
Remember one sentence: def writes the card, and a call follows it. To look it up, see section 4.8 of the Python 3.13 tutorial, on defining functions.
2 Picture it
A table of three parts of a program and what each does. The def line names the body. The body waits to be run. A call runs the body. The row for the call is marked.
3 Why
Python reads a file from the top. When it reaches a def line it stores the body under the function's name and moves on to the first line after the body. Nothing is displayed, because the body has not run.
At a call of a function that has been defined, Python goes to the stored body, runs its lines in order and then carries on from the place of the call. The banner program calls show_banner twice, so the three lines of its body run twice.
A name has to have a value before it is used. In a file where nothing else has defined show_banner, a call that runs before the def line stops the program with a NameError.
4 The rule
A def line and its indented body define a function without running the body.
The functions of this lesson have no parameters. Once such a function has been defined, a call, written as its name followed by an empty pair of parentheses, runs the body from its first line. When the body finishes, the program carries on from the place of the call. Each such call runs the body once.
5 How
- Find the lines that the program needs in more than one place.
- Write
def, a name that says what the lines do, an empty pair of parentheses and a colon. - Put the lines under the
defline and indent them one more level, four spaces, keeping any deeper indentation they already have. - Leave the indented block, and write the function's name with parentheses at each place where the lines should run.
- Trace the program: skip the body at the
defline, and run the body at each call.
6 Worked example
Call one function twice
def show_banner():
print("============")
print("LOST AND FOUND")
print("============")
show_banner()
print("Blue scarf")
show_banner()
Standard input
Empty
Standard output
============
LOST AND FOUND
============
Blue scarf
============
LOST AND FOUND
============
Predict the seven output lines first. Then save the code as banner.py and run the file, or follow the trace below. The body runs once for each call.
Call the function too early
show_banner()
def show_banner():
print("LOST AND FOUND")
Standard input
Empty
Standard output
Empty
Exception NameError
Save the four lines as early.py and run the file. The call is on line 1, above the def line, so Python does not know the name yet and stops with a NameError. Move the call below the function to repair the program.
Trace
| Instruction | What Python does | Output | Explanation |
|---|---|---|---|
def show_banner(): | Stores the three body lines under the name | — | Nothing is displayed. Python moves to the first line after the body. |
show_banner() | Runs the body | the three banner lines | The first call. The body's three print lines run in order. |
print("Blue scarf") | Runs | Blue scarf | Python has come back from the call and carries on. |
show_banner() | Runs the body again | the three banner lines | The second call runs the same three lines. |
7 The trap
The parentheses make the call. On a line below the function's definition, the name alone, show_banner with no parentheses, does not call it. The file displays no banner and reports no error. Check for the parentheses when a function seems to do nothing.
The body belongs to the function only while it is indented. A print line at the margin, after the body, is not part of the function. It runs once, when Python reaches it, and not at each call.
Quick checks
Quick check 1 of 2
Predict what this program displays. The program reads no input.
def chime():
print("ding")
print("start")
chime()
chime()
print("end")Show the steps and the answer
It displays start, ding, ding and end, each on its own line.
Why: The def line displays nothing. The first print runs, then each call runs the body once, then the last print runs.
Source: Python Software Foundation, Python 3.13 Language Reference, 8.7 Function definitions
Quick check 2 of 2
A file holds only these two lines. What does it display when it is run?
def hello():
print("hello")Show the steps and the answer
It displays nothing.
Why: The file defines the function and never calls it. The body runs only at a call.
Source: Python Software Foundation, Python 3.13 Language Reference, 8.7 Function definitions
Sources
- Python 3.13 Tutorial, 4.8 Defining Functions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 8.7 Function definitions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 6.3.4 Calls. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Library Reference, Built-in Exceptions: NameError. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Tutorial, 4.10 Intermezzo: Coding Style. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 7.1 Expression statements. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.