﻿1
00:00:00,440 --> 00:00:04,220
‫So in this video, we're going to talk about the call stack.

2
00:00:04,490 --> 00:00:09,830
‫Now, earlier in the course, we had a data structure called a stack and we said it was like a can of

3
00:00:09,830 --> 00:00:10,940
‫tennis balls.

4
00:00:11,270 --> 00:00:17,670
‫And a lot of the principles of stacks that we discussed previously will apply to call stacks.

5
00:00:17,690 --> 00:00:24,290
‫So, for example, you can only get to the item at the top of the call stack, and once that is popped,

6
00:00:24,290 --> 00:00:27,440
‫you can get to the next item and so on.

7
00:00:27,770 --> 00:00:33,200
‫So I'm going to take this container and move it over here and bring in a function.

8
00:00:33,230 --> 00:00:38,690
‫I'm going to call this function one and all it does is output this string one.

9
00:00:39,080 --> 00:00:46,730
‫But when we call that function, that function gets added to the call stack and then after it outputs

10
00:00:46,730 --> 00:00:53,720
‫one, this function is done running and function one gets popped from the call stack.

11
00:00:54,320 --> 00:00:59,450
‫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

12
00:00:59,450 --> 00:01:00,200
‫function.

13
00:01:00,590 --> 00:01:03,680
‫And with that line, we're going to call function two.

14
00:01:04,040 --> 00:01:10,280
‫Well, once you call function to function two becomes the top item on the call stack.

15
00:01:10,550 --> 00:01:17,990
‫And once function two is added to the call stack, function, one stops running at pauses and it can

16
00:01:17,990 --> 00:01:23,450
‫only start running again once function two is popped from the call stack.

17
00:01:23,840 --> 00:01:28,910
‫So now we're going to add a line to function two where it calls function three.

18
00:01:29,240 --> 00:01:37,400
‫Now function three is at the top of the call stack, and neither function one nor function two are running.

19
00:01:37,910 --> 00:01:40,940
‫And we're not going to add anything to this function.

20
00:01:40,940 --> 00:01:44,390
‫We're just going to have it output three to the screen.

21
00:01:44,720 --> 00:01:51,110
‫And once it does this, this function is done running and function three is popped from the call stack.

22
00:01:51,500 --> 00:01:57,380
‫Now function two can run again and function two just got done running this line.

23
00:01:57,380 --> 00:02:02,120
‫Now it moves to this line and outputs two to the console.

24
00:02:02,510 --> 00:02:07,220
‫Once it does, this function two will be popped from the call stack.

25
00:02:07,520 --> 00:02:10,280
‫Now function one can run again.

26
00:02:10,550 --> 00:02:13,430
‫It just got done running this line of code.

27
00:02:13,790 --> 00:02:21,830
‫Now it moves to the next line of code and outputs one to the screen and that is popped from the call

28
00:02:21,830 --> 00:02:22,460
‫stack.

29
00:02:22,910 --> 00:02:30,530
‫So notice that the functions were called one, two, three, but the output is 3 to 1.

30
00:02:30,860 --> 00:02:32,750
‫That's because that's the order.

31
00:02:32,750 --> 00:02:35,120
‫These came off of the call stack.

32
00:02:35,720 --> 00:02:42,130
‫So I'm going to bring our three functions up and we'll flip over and look at these NVS code.

33
00:02:42,670 --> 00:02:45,280
‫So there are the three functions there.

34
00:02:45,280 --> 00:02:51,460
‫And then in our main function, that is where we do the initial function call of function one.

35
00:02:51,940 --> 00:02:57,370
‫So if we run this, we get 3 to 1 just like we would expect.

36
00:02:57,640 --> 00:03:02,890
‫But let's take a look at how these go on to and come off of the call stack.

37
00:03:03,130 --> 00:03:05,950
‫In order to do that, I'm going to come over here.

38
00:03:06,310 --> 00:03:11,770
‫I'm going to click on this button where it says Run and debug and we'll get this side pane.

39
00:03:11,770 --> 00:03:17,410
‫And you can see right here it says Call Stack, and then I'm going to come down here to the left of

40
00:03:17,410 --> 00:03:22,540
‫the number that's next to where it says function one in the main function.

41
00:03:22,540 --> 00:03:26,200
‫And you can see we get a red dot, a dark red dot.

42
00:03:26,200 --> 00:03:30,760
‫But if I actually click this, it turns into a bright red dot.

43
00:03:30,940 --> 00:03:34,420
‫And what that creates is what is called a breakpoint.

44
00:03:34,660 --> 00:03:38,110
‫So the code will only run up to that point and stop.

45
00:03:38,740 --> 00:03:47,470
‫And then I'll come up here to this dropdown and I'll select debug C and C++ file.

46
00:03:47,890 --> 00:03:55,120
‫And when I do this, what I want to do is ignore what is on the terminal there, come up to the three

47
00:03:55,120 --> 00:04:00,430
‫dots, click on this and choose debug console.

48
00:04:00,610 --> 00:04:05,500
‫We'll click on that and you're going to get a bunch of information here that you can ignore.

49
00:04:05,500 --> 00:04:09,220
‫I'm going to right click and say clear console.

50
00:04:09,220 --> 00:04:14,320
‫And you can see when I did this that this line of code is now highlighted.

51
00:04:14,320 --> 00:04:18,070
‫That means that's the line of code that is about to run.

52
00:04:18,370 --> 00:04:21,910
‫And on our call stack we have the main function.

53
00:04:21,910 --> 00:04:24,190
‫That's the function we're in down here.

54
00:04:24,850 --> 00:04:32,530
‫And then up here we have this button that says, step into this downward facing button.

55
00:04:33,260 --> 00:04:39,800
‫So I'll zoom back out here and when I click on this, it's going to run the next line of code, which

56
00:04:39,800 --> 00:04:42,290
‫is going to be function one down here.

57
00:04:42,290 --> 00:04:48,140
‫So I'll click on this and you can see that function one is added to the call stack and because it's

58
00:04:48,140 --> 00:04:53,990
‫at the top of the call stack, that is the function that is running now here because function two is

59
00:04:53,990 --> 00:04:54,530
‫highlighted.

60
00:04:54,530 --> 00:05:01,460
‫That means that's the next line of code that's about to run and I'm going to click Step Into Again and

61
00:05:01,460 --> 00:05:04,670
‫you can see that function two has been added to the call stack.

62
00:05:04,790 --> 00:05:07,990
‫This is the next line to run function three.

63
00:05:08,000 --> 00:05:14,030
‫I'll click on Step Into Again and now Function three has been added to the call stack.

64
00:05:14,270 --> 00:05:22,280
‫The next line to run is going to be this and when I click on step into it will output three to that

65
00:05:22,280 --> 00:05:23,930
‫debug console.

66
00:05:24,110 --> 00:05:31,550
‫Now function three is done running and when I click step into a couple more times it is removed from

67
00:05:31,550 --> 00:05:32,600
‫the call stack.

68
00:05:32,870 --> 00:05:38,690
‫Now function two picks up where it left off and now it's going to output two.

69
00:05:38,690 --> 00:05:46,430
‫So I'll click step into again that gets output to the debug console there and then I'll click step into

70
00:05:46,430 --> 00:05:51,110
‫a couple more times and function two has been removed from the call stack.

71
00:05:51,350 --> 00:05:56,990
‫So now with this line, we'll output the number one I'll click step into and that has been output up

72
00:05:56,990 --> 00:06:01,160
‫here and now function one is about to be popped from the call stack.

73
00:06:01,430 --> 00:06:09,290
‫I'll click on step into a couple of times and now that has been removed and 3 to 1 has been output to

74
00:06:09,290 --> 00:06:10,790
‫the debug console.

75
00:06:11,400 --> 00:06:18,300
‫So while this example is not recursive, I wanted to do it this way because it makes it easier to understand

76
00:06:18,300 --> 00:06:20,820
‫the concept of the call stack.

77
00:06:21,150 --> 00:06:28,200
‫But in the next video, we will look at a recursive example of how things go onto and come off of the

78
00:06:28,200 --> 00:06:34,140
‫call stack as we get into the course, especially with the recursive functions.

79
00:06:34,380 --> 00:06:40,770
‫I'm going to recommend that you create a breakpoint and step through this to see how it is all going

80
00:06:40,770 --> 00:06:43,590
‫onto and off of the call stack.

81
00:06:43,980 --> 00:06:49,040
‫But for now, that is our quick introduction of the call stack.

