Trace One Function Call
Follow an argument into mean(), trace its fresh local total and count, and see only the returned value cross back to the caller.
The mean function may be called for several sets of readings. Each call must
begin with a fresh total and count; otherwise an earlier report would change a
later one.
The names created for one call are local names. The part of the program in which a name can be used is its scope.
Draw a Boundary Around One Call
First trace this call:
first_mean = mean([18, 22])
| Step | Local readings | Local reading | Local total | Local count | Value returned |
|---|---|---|---|---|---|
| bind argument | [18, 22] | — | — | — | — |
| initialize | [18, 22] | — | 0 | 0 | — |
| first iteration | [18, 22] | 18 | 18 | 1 | — |
| second iteration | [18, 22] | 22 | 40 | 2 | — |
| return | [18, 22] | 22 | 40 | 2 | 20.0 |
The parameter readings, loop name reading, and assigned names total and
count are local to this call. The returned 20.0 crosses the function
boundary and becomes the value of first_mean in the caller.
A Second Call Starts Fresh
Now call the same function with different readings:
second_mean = mean([10, None, 14])
| Step | Local readings | Local reading | Local total | Local count | Value returned |
|---|---|---|---|---|---|
| bind argument | [10, None, 14] | — | — | — | — |
| initialize | [10, None, 14] | — | 0 | 0 | — |
| first iteration | [10, None, 14] | 10 | 10 | 1 | — |
| skip unavailable value | [10, None, 14] | None | 10 | 1 | — |
| third iteration | [10, None, 14] | 14 | 24 | 2 | — |
| return | [10, None, 14] | 14 | 24 | 2 | 12.0 |
The second call does not begin with the first call's total = 40. Its own
initialization sets total = 0 and count = 0. The two calls use the same
definition but have separate local work.
Q1. Finish one call trace
What does this call return?
mean([16, None, 20])
Compute it first, then check your number.
HintKeep this call separate
Its available readings are 16 and 20; no value from an earlier call enters the calculation.
SolutionThe call returns eighteen
The call creates total = 36 and count = 2, then returns 36 / 2, which
is 18.0.
The Same Spelling Can Appear in Two Scopes
The caller may also use the name readings:
The first readings belongs to the surrounding script. During the call, the
parameter named readings is local to mean. The argument supplies its value;
it does not make every name inside the function available outside.
After the call, the surrounding code can use result. It cannot use the local
total, count, or reading from the function body:
The returned result is the deliberate connection between the two scopes. Intermediate names stay inside the function.
Q2. Identify the local names
Which names are local to mean during a call?
Select every choice that applies, then check.
HintLook inside the definition
Parameters are local names. So are the loop name and names assigned in the function body.
SolutionFour names belong to the call
readings, reading, total, and count are local to mean.
morning_readings and morning_mean belong to the surrounding caller.
Make Every Changing Input Explicit
A function can silently read a name from the surrounding program:
The call adjust_mean(20) does not show that its result also depends on
adjustment. Pass the changing value explicitly instead:
Now each call states both inputs, and the function no longer relies on hidden state. This is easier to trace because the function boundary shows the complete data flow.
Q3. Expose the hidden input
Repair the function so each call supplies its own adjustment. The program should display 21 and 23.
Editable Python
Ready to run.
HintMove the dependency into the parameter list
Define adjust_mean(value, adjustment), then call it once with 1 and once
with 3.
SolutionPass both inputs
Each call binds explicit inputs to fresh local names and exposes only its returned result. With that boundary clear, defaults and keyword arguments can make optional choices readable without hiding the data flow.