Professional Documents
Culture Documents
16 Call Stacks
16 Call Stacks
seemed a little bit magical. How does it work? How do the functions know that they
need to wait and wait for another value to return from a different function call in
order to get the result we want? Well, the reason this works is because of
something known as the call stack. When you call a function, the system sets aside
space in memory for that function to do its work. And we call these chunks of
memory that are being set aside for each function call a stack frame or a function
frame. And as you might expect, these stack frames live on the stack part of
memory.
More than one function stack frame can exist in memory at a given time. If main
calls a function move, and move calls direction, all three functions have open
frames. But they don't all have active frames. These frames are arranged in a
stack. And the frame from the most recently called function is always on top of the
stack. And that is always the active frame. There's only really ever one function
that's active at a time. It's the one on top of the stack.
And if it calls another function, it's going to pause again. That new function's
stack frame will be pushed onto the top of the stack. It'll do its work. It might
pop back off. And the other function below it can resume again.
So let's go through this again, looking at the idea of the factorial function that
we defined in the recursion video to see exactly how the magic behind this
recursive process is taking place. So this is our entire file, right? We defined
two functions-- main and fact. And as we might expect, any C program is going to
start at the first line of main.
So we create a new stack frame for main. And it's going to begin running. Main
calls printf. And printf is going to print out factorial of 5. Well, it doesn't
know what factorial of 5 is, and so this call is already depending on another
function call. So main is going to pause right there. I'm gonna leave its arrow
right there, color it the same color as the stack frame on the right, to indicate
that main is going to freeze here while factorial of 5 is called.
So factorial of 5 is called. And it's going to start at the very beginning of the
factorial function. It asks the question am I equal to 1? Is 5 equal to 1? Well,
no. So it's going to go down to the else part, return n times factorial of n minus
1. Well, OK.
And so it needs to call factorial of 3. And that's gonna go through the same
process again. It starts through, gets here. Factorial of 3 depends on factorial of
1. So factorial of 2 starts, gets here. It depends on factorial of 1. Factorial of
1 starts.
And it can pick up exactly where it left off. It's been waiting for a factorial of
1 to finish its work. It has now finished. And so here we are.
4 says, OK, I'm going to return 4 times the factorial of 3, which was six. That was
of value that factorial of 3 returned. And so 4 times 6 is 24. And I'm going to
pass that back to factorial of 5, which has been waiting for me. Factorial of 5 is
now the active frame. It's going to return 5 times factorial of 4-- 5 times 24, or
120-- and give that value back to main, which has been waiting very patiently for a
long time at the bottom of the stack.
It's where it started. It made this call. Several frames took over at the top. It
is now back on top of the stack. It's the active frame. So main got the value 120
back from factorial of 5. It's been waiting to print out that value. And then it's
done. There's no more lines of code in main. So main's frame pops off the stack,
and we're done.
And that's how recursion works. That's how stack frames work. Those function calls
that happened previously are just on pause, waiting for the subsequent calls to
finish so they can become the active frame and finish what they need to do.