Base CS from zero
Functions and the call stack: multiple-choice review
Six questions that cut across the whole unit. Each one asks you to reason about what the machine actually does when a function is called — not to recite a definition, but to predict the state of the stack, the path of control, and where data lives.
Confirm you can connect the call mechanism, the stack frame, parameter passing, scope and lifetime, and stack overflow into one coherent model of how a function call works at the machine level.
A function body lives at one address. The same function is called from three different places in the program. How does each call return to the right caller?
main calls outer, outer calls inner. While inner is executing, what is on the stack and in what order?
A function add(x, y) is called as add(3, 5). Where do the values 3 and 5 live while add runs, and what does that guarantee?
Function f declares a local variable result. After f returns, code in main tries to use result. What is true?
countdown(n) returns when n < 0 and otherwise calls countdown(n - 1). What is the maximum number of frames on the stack at once for countdown(3)?
A recursive function is shipped without a reachable base case. What happens at runtime, and why so fast?
The unit’s through-line is one mechanism seen from several angles: a CALL saves a return address and jumps to a fixed body; each call pushes a LIFO stack frame holding that return address plus the function’s locals; parameters are copied in at CALL and the result handed back at RET; a local’s scope and lifetime are bounded by its frame, so it vanishes on return; and recursion is just the same frame-push repeated, made safe only by a reachable base case — without one, the frames pile up until the stack overflows.