Tracing the call stack
≈ 45 minTracing the call stack
A recursive trace has two directions. On the way down, each call stores its local variables and waits for a smaller call. On the way up, each waiting call receives a returned value and completes its own calculation. Drawing frames is safer than mentally jumping from the first call to the final answer because it exposes the exact argument and return value at each level.
The call stack is limited. A recursion that decreases by one across millions of values may be mathematically correct yet fail in practice because there are too many frames. For a Grade 12 trace, first identify the base case, then record the call chain, then unwind. If the base case is skipped, the trace should stop with an error rather than invent a result.
Worked reasoning. Trace countDigits(402). The first call sees 402 and waits for 1 plus countDigits(40). The next waits for 1 plus countDigits(4). The final non-zero digit waits for countDigits(0), which returns 0. Returns then make 1, 2 and 3.
Exam lens. Use a call-and-return table with columns for argument, stopping condition and returned value. This is clearer than writing arrows with no labels.
Which statement is the most defensible principle for Tracing the call stack?
Enter the key term for Tracing the call stack. What structure stores unfinished method calls until a recursive call returns?
In a trace of reverse(text, i), which information must each call keep while it waits for reverse(text, i + 1)?
Name the concise safeguard or principle that completes this lesson’s scenario: In a trace of reverse(text, i), which information must each call keep while it waits for reverse(text, i + 1)?

