Sub topic 12
Tracing Procedure Calls with a Call Stack
This chapter assumes you can already build a trace table and trace a single loop. It also assumes you know how procedures take parameters and return values. Here you extend the trace table so it can follow a program that jumps from one procedure into another and back again.
The call stack model
A call stack is the list of procedure calls that have started but not yet returned. Each call gets its own frame. A frame is a record that holds the call's parameters, its local variables, and the point in the caller where execution resumes.
The rules are short:
- When a procedure is called, push a new frame on top of the stack and copy the argument values into its parameters.
- Only the top frame runs. Variables in lower frames are paused and cannot be seen.
- When the top frame reaches
RETURN, pop it off the stack and hand the return value back to the exact expression that made the call.
A worked example
FUNCTION Sum(A, n)
total ← 0
FOR i ← 1 TO n DO
total ← total + A[i]
END FOR
RETURN total
END FUNCTION
FUNCTION Average(A, n)
s ← Sum(A, n)
RETURN s / n
END FUNCTION
result ← Average([4, 6, 8], 3)To trace this, add a stack column that lists active frames from bottom to top. Write each variable with its frame's name so you never confuse two variables that share a name.
step | stack | event | variables changed
-----+-----------------------+---------------------------+---------------------------
1 | main | call Average([4,6,8], 3) | -
2 | main, Average | call Sum(A, 3) | Average.n = 3
3 | main, Average, Sum | init | Sum.n = 3, Sum.total = 0
4 | main, Average, Sum | i = 1 | Sum.total = 4
5 | main, Average, Sum | i = 2 | Sum.total = 10
6 | main, Average, Sum | i = 3 | Sum.total = 18
7 | main, Average | Sum returns 18 | Average.s = 18
8 | main | Average returns 6 | main.result = 6In step 7 the Sum frame disappears, and total and i go with it. The value 18 lands in s, the variable on the left of the call in Average.
Common tracing errors
| Error | Symptom in the trace | Fix |
|---|---|---|
| Sharing variables across frames | A caller's variable changes when a callee assigns a variable with the same name | Prefix every variable with its frame name |
| Losing the return point | The trace continues on the wrong line after a return | Note the calling line when you push a frame |
| Keeping dead locals | A later call sees an old value of `total` | Cross out a frame's variables when you pop it |
| Assuming parameters alias arguments | A scalar argument changes in the caller | Copy scalar values into the new frame |
Exercise
Trace this program with a call stack. Scalars are passed by value. What are the final values of main.x and main.y? FUNCTION Double(x) x ← x × 2 RETURN x END FUNCTION x ← 5 y ← Double(x)
Show answer
main.x = 5 and main.y = 10. The call pushes a Double frame whose parameter Double.x starts as a copy of 5. Assigning Double.x ← 10 changes only that frame. The frame returns 10 into main.y and is popped, which leaves main.x unchanged at 5.
Exercise
In the Average example, what does the stack column contain at the moment Sum.total first becomes 10, and which variables are visible then?
Show answer
The stack is main, Average, Sum. Only Sum's variables are visible: A, n = 3, total = 10, and i = 2. Average.s has not been assigned yet because Sum has not returned.