Each function call gets a stack frame, a region of memory holding its parameters, local variables, and a return address pointing back to the caller's next instruction. When a function calls another (including itself), a new frame is pushed on top. When it returns, the frame is popped, and execution resumes in the caller. During recursion the stack grows linearly with depth: factorial(100) produces 100 stacked frames before any of them return. Once the innermost call hits the base case and returns, the frames unwind in reverse order, each one finishing its remaining work (e.g., the multiplication in n * factorial(n - 1)). The stack has a finite size, typically 1-8 MB, so very deep recursion crashes with a stack overflow.
Programming Fundamentals · Interview question
How does the call stack work during recursion?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Recursion
A function that calls itself. The base case, the recursive case, the call stack, and the question of when to use recursion vs a loop.