Skip to content

Scope and the call stack

Trace a program with one frame for each function call, say which names each call can read, and explain why a local variable is gone after its function returns.

It opens on

In this program cost calls area, and both functions have parameters named w and h. While area runs, is there one w and one h in the whole program, or two of each?

Why it matters

When two functions use the same names, or a function seems to change a variable that is not its own, or a name that was set inside a function is missing outside it, the cause is always where each name lives, and the call stack of frames is the picture that settles it.

Good to know first

  • Defining and calling functions: a call binds arguments to parameters and returns a value
  • Variables and assignment: a name is bound to a value by assignment
  • Values, types and expressions: result = w * h computes a value from names

Step by step

  1. One frame for each call

    Draw the stack of frames at a moment of a program with nested calls, and say which frames exist and what each holds.

    Line 8 calls cost(3, 4), so a frame for cost is put on the stack with w equal to 3 and h equal to 4. Then line 6 calls area(w, h), and a second frame, for area, goes on top of it, with its own w equal to 3 and h equal to 4.

    A common mix-up

    There is one w in the program, so area and cost share it.

    In fact: Each call has its own frame, so each has its own w and h, which do not affect each other.

  2. Local names, global names and two errors

    Say whether a name in a function is local or global, predict the NameError and UnboundLocalError, and explain why a local name is gone after the return.

    The global count is not changed by this, so the last line prints 10, and the program shows 11 and then 10. Reading a global name from inside a function is allowed.

    A common mix-up

    Assigning to count inside a function updates the global count.

    In fact: The assignment makes a new local count, which is gone at the return and which, when read before it is set, gives an UnboundLocalError.

  3. The same name in three frames

    Trace a program in which the same name x is a parameter in two functions and a global, and give the value of each x and of the printed result.

    Line 7 calls g(3), so a frame for g is made, with its parameter x equal to 3. Then g calls f(x), which passes the value 3, so a frame for f is made on top, and its own x is 3.

    A common mix-up

    A function that changes its parameter x changes the x it was called with.

    In fact: The parameter is a separate name in the function's own frame, so the caller's x is untouched.

Try it

With def sq(x): return x * x and def f(a): return sq(a) + 1, how many frames are there at the deepest point of print(f(3)), and what does it print?Answer

3 frames, global, f and sq; it prints 10

Hint: The deepest point is while sq runs

With def h(): k = 3, then k = k + 1, then return k, what do print(h()) and then print(k) do, if there is no k anywhere else?Answer

The first prints 4, and the second stops with a NameError

Hint: Where does k live?

With x = 1, def f(x) returning x * 10 after x = x * 10, what does print(f(2), x) show?Answer

20 1

Hint: f's x is a different name from the global x

If the body of area changed its own w to 99, would the w in the cost frame change?Answer

No, each frame has its own w, so cost's w stays 3.

What does the last line, print(z), do?Answer

It stops with a NameError, saying that z is not defined, because no frame has a z any more.

Scope and the call stack

30 minutes every month. No card.