Pop a frame on return
Updated
Return pops the current frame and resumes the buried caller with the returned value. The caller's locals are as they were. Forgetting to pop leaks frames. Popping twice drops the caller.
What to focus on
Explicit: result = stack.pop(); continue the new top. Base case pops without pushing another call.
Worked examples
return to caller
a = [f(4), f(1)], return 1- pop f(1)
- resume f(4) with 1
Result: [f(4)]
return last
a = [f(4)], return 24- pop f(4)
- empty call stack
Result: []
empty
a = []- no frame to return from
Result: bug — return without call
Try it
Write popFrame(stack). stack is the non-empty call stack. Pop exactly one frame. Return the new stack. Resume the buried caller. a = [f(4), f(1)], return 1 → [f(4)]. a = [f(4)], return 24 → []. a = [] → bug — return without call.
Returning on an empty call stack is a bug. Empty is in the cases. Each return pops exactly one frame.