Extract Familiar Work into a Function
Move the Chapter 3 measurement summary into show_summary(), distinguish a definition from a call, and trace when its indented body runs.
The previous chapter ended with a program that skips an unavailable reading, keeps a total and count, and prints a mean:
If another part of a program needs the same calculation, copying all these lines would create two places to read and repair. A function gives the connected work one name.
Give the Familiar Work a Name
Move the working program beneath a function header and indent it as one block:
Nothing inside the body is new. The header names the work:
| Part | Meaning here |
|---|---|
def | begin a function definition |
show_summary | name the function |
() | show that this function currently receives no input |
: | begin the function body |
| indented lines | instructions that belong to the function |
The name describes the visible job. show_summary says more than a vague name
such as work or do_it.
Defining Does Not Run the Body
When Python reaches the definition, it records the function. It does not yet calculate or display the report. The body runs when Python reaches a call:
show_summary()
The complete script defines the function before calling it:
Its output is:
Report
Mean: 20.5 °C
Finished
Trace the script in this order:
- Python records the definition without entering its body.
print("Report")runs.show_summary()enters the function and runs its body.- When the body ends, execution continues after the call.
print("Finished")runs.
Q1. Separate definition from execution
What does this program display?
Select one choice, then check.
HintCount calls, not definitions
Python reaches show_summary() twice after printing ready.
SolutionEach call runs one body
Python records the function, prints ready, and then enters the body for
each of the two calls. The two calls display summary twice.
Indentation Marks the Body
Every instruction that belongs to the function uses the same additional level
of indentation. A line aligned with def is outside the body:
The blank line improves readability but does not end the body by itself. The
return to the earlier indentation level shows that print("Finished") belongs
to the surrounding script.
Q2. Find the surrounding instruction
Which instruction is outside show_summary?
Select one choice, then check.
HintWatch where indentation returns
The body lines begin four spaces to the right of def.
SolutionThe final print surrounds the function
print("report complete") is aligned with the definition. It runs as part
of the surrounding script, whether or not the function is called.
Parentheses Make the Call
The name and the call are different expressions:
show_summary refers to the function. show_summary() calls it. In this
chapter, when the intention is to run a function, look for the call
parentheses.
Python must also record the definition before it reaches the call. Calling
show_summary() above its definition fails because the name does not yet refer
to the function. Moving the complete definition above the call repairs the
order.
Q3. Run the fixed summary
The function is already defined. Add the missing call so the program displays
Mean: 20.5 °C once.
Editable Python
Ready to run.
HintUse the function name with parentheses
Write show_summary() after the body ends.
SolutionCall after defining
show_summary()
Calling show_summary() again would run the same calculation, but it would
also use the same fixed readings. The next step is to let each call supply its
own readings and receive the calculated result.
A function definition gives familiar work a name; a call runs its indented body. Our first function still owns fixed data, which makes the need for an explicit input and returned result visible.