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])
StepLocal readingsLocal readingLocal totalLocal countValue returned
bind argument[18, 22]
initialize[18, 22]00
first iteration[18, 22]18181
second iteration[18, 22]22402
return[18, 22]2240220.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])
StepLocal readingsLocal readingLocal totalLocal countValue returned
bind argument[10, None, 14]
initialize[10, None, 14]00
first iteration[10, None, 14]10101
skip unavailable value[10, None, 14]None101
third iteration[10, None, 14]14242
return[10, None, 14]1424212.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.

Not attempted
Review

Not marked done.

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 local name

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.

Not attempted
Review

Not marked done.

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

Command/Ctrl + Enter. Python runs in your browser.

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
Not attempted
Review

Not marked done.

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.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.