‫So in this video, we're going to talk about the call stack.
‫Now, earlier in the course, we had a data structure called a stack and we said it was like a can of
‫tennis balls.
‫And a lot of the principles of stacks that we discussed previously will apply to call stacks.
‫So, for example, you can only get to the item at the top of the call stack, and once that is popped,
‫you can get to the next item and so on.
‫So I'm going to take this container and move it over here and bring in a function.
‫I'm going to call this function one and all it does is output this string one.
‫But when we call that function, that function gets added to the call stack and then after it outputs
‫one, this function is done running and function one gets popped from the call stack.
‫So I'm going to bring this back and put it back on our call stack and we're going to add a line to this
‫function.
‫And with that line, we're going to call function two.
‫Well, once you call function to function two becomes the top item on the call stack.
‫And once function two is added to the call stack, function, one stops running at pauses and it can
‫only start running again once function two is popped from the call stack.
‫So now we're going to add a line to function two where it calls function three.
‫Now function three is at the top of the call stack, and neither function one nor function two are running.
‫And we're not going to add anything to this function.
‫We're just going to have it output three to the screen.
‫And once it does this, this function is done running and function three is popped from the call stack.
‫Now function two can run again and function two just got done running this line.
‫Now it moves to this line and outputs two to the console.
‫Once it does, this function two will be popped from the call stack.
‫Now function one can run again.
‫It just got done running this line of code.
‫Now it moves to the next line of code and outputs one to the screen and that is popped from the call
‫stack.
‫So notice that the functions were called one, two, three, but the output is 3 to 1.
‫That's because that's the order.
‫These came off of the call stack.
‫So I'm going to bring our three functions up and we'll flip over and look at these NVS code.
‫So there are the three functions there.
‫And then in our main function, that is where we do the initial function call of function one.
‫So if we run this, we get 3 to 1 just like we would expect.
‫But let's take a look at how these go on to and come off of the call stack.
‫In order to do that, I'm going to come over here.
‫I'm going to click on this button where it says Run and debug and we'll get this side pane.
‫And you can see right here it says Call Stack, and then I'm going to come down here to the left of
‫the number that's next to where it says function one in the main function.
‫And you can see we get a red dot, a dark red dot.
‫But if I actually click this, it turns into a bright red dot.
‫And what that creates is what is called a breakpoint.
‫So the code will only run up to that point and stop.
‫And then I'll come up here to this dropdown and I'll select debug C and C++ file.
‫And when I do this, what I want to do is ignore what is on the terminal there, come up to the three
‫dots, click on this and choose debug console.
‫We'll click on that and you're going to get a bunch of information here that you can ignore.
‫I'm going to right click and say clear console.
‫And you can see when I did this that this line of code is now highlighted.
‫That means that's the line of code that is about to run.
‫And on our call stack we have the main function.
‫That's the function we're in down here.
‫And then up here we have this button that says, step into this downward facing button.
‫So I'll zoom back out here and when I click on this, it's going to run the next line of code, which
‫is going to be function one down here.
‫So I'll click on this and you can see that function one is added to the call stack and because it's
‫at the top of the call stack, that is the function that is running now here because function two is
‫highlighted.
‫That means that's the next line of code that's about to run and I'm going to click Step Into Again and
‫you can see that function two has been added to the call stack.
‫This is the next line to run function three.
‫I'll click on Step Into Again and now Function three has been added to the call stack.
‫The next line to run is going to be this and when I click on step into it will output three to that
‫debug console.
‫Now function three is done running and when I click step into a couple more times it is removed from
‫the call stack.
‫Now function two picks up where it left off and now it's going to output two.
‫So I'll click step into again that gets output to the debug console there and then I'll click step into
‫a couple more times and function two has been removed from the call stack.
‫So now with this line, we'll output the number one I'll click step into and that has been output up
‫here and now function one is about to be popped from the call stack.
‫I'll click on step into a couple of times and now that has been removed and 3 to 1 has been output to
‫the debug console.
‫So while this example is not recursive, I wanted to do it this way because it makes it easier to understand
‫the concept of the call stack.
‫But in the next video, we will look at a recursive example of how things go onto and come off of the
‫call stack as we get into the course, especially with the recursive functions.
‫I'm going to recommend that you create a breakpoint and step through this to see how it is all going
‫onto and off of the call stack.
‫But for now, that is our quick introduction of the call stack.