1
00:00:00,540 --> 00:00:08,520
So we're going to have one last lecture on functions I was hoping to be able to move into the other

2
00:00:08,520 --> 00:00:16,140
topics by now, but there are a couple sub topics that have to do with functions that I do need to mention.

3
00:00:16,440 --> 00:00:18,750
So this is going to be a short lecture.

4
00:00:19,230 --> 00:00:22,830
Don't worry, I won't be stuffing a ton of information into this.

5
00:00:23,250 --> 00:00:27,750
I just have to cover like two last little things with functions.

6
00:00:27,750 --> 00:00:29,610
So that's why we have this part three.

7
00:00:30,270 --> 00:00:31,890
So let's go over those two things.

8
00:00:33,090 --> 00:00:35,070
So the first thing is something called prototypes.

9
00:00:35,100 --> 00:00:36,930
Well, what are the prototypes?

10
00:00:37,050 --> 00:00:38,430
Well, think about it this way.

11
00:00:38,430 --> 00:00:43,470
The compiler must know about a functions definition before it's called.

12
00:00:43,830 --> 00:00:47,460
So think back to what a function definition is because we just went over that right.

13
00:00:47,760 --> 00:00:53,060
The definition is where you put all of the actual code that's meaningful, that the function runs right.

14
00:00:53,070 --> 00:00:55,800
It has the curly braces and all that.

15
00:00:56,430 --> 00:01:00,540
The function call just has the name with the parentheses and the arguments.

16
00:01:00,540 --> 00:01:02,820
Right definition has the parameters.

17
00:01:03,120 --> 00:01:03,690
Vesta.

18
00:01:04,680 --> 00:01:13,110
So if we're doing things tough down and then we have a function that is defined below main notice that

19
00:01:13,680 --> 00:01:20,350
we've only defined the functions above Maine so far, and this is actually important point.

20
00:01:21,540 --> 00:01:26,970
If we were to instead define it below Maine, when we call that function in Maine, the compiler will

21
00:01:26,970 --> 00:01:30,090
not know about the function definition yet.

22
00:01:31,340 --> 00:01:35,660
So at the time that it sees that call to the function, it's not going to know what it is and it's going

23
00:01:35,660 --> 00:01:36,500
to throw an error.

24
00:01:36,500 --> 00:01:43,430
So the compiler is going to throw an error because what it will be thinking is, Oh, this is not within

25
00:01:43,430 --> 00:01:45,080
the scope, right?

26
00:01:45,710 --> 00:01:51,230
We talked about scope within like, you know, if condition or a loop, you know, stuff inside of those

27
00:01:51,230 --> 00:01:52,130
curly braces.

28
00:01:52,130 --> 00:01:54,560
Also for a function, there's a scope, right?

29
00:01:54,950 --> 00:01:58,700
We talked about in Maine's scope or in another function scope.

30
00:01:58,700 --> 00:02:05,540
It's all the scope is whatever falls between the curly braces, you know, and what that function can

31
00:02:05,540 --> 00:02:10,040
recognize, except Maine doesn't recognize anything is just going to be like there was nothing in my

32
00:02:10,040 --> 00:02:11,840
scope that makes me recognize this.

33
00:02:12,820 --> 00:02:17,830
There is a way to fix that, though, since we haven't hit the definition yet, but we want to call

34
00:02:17,830 --> 00:02:21,440
it in Maine, and let's say we want to leave the definition below Maine.

35
00:02:21,460 --> 00:02:26,350
It is possible we just have to use something called a prototype or a function prototype.

36
00:02:27,580 --> 00:02:29,860
So I will show you that here on this next page.

37
00:02:30,220 --> 00:02:34,390
So this is what the functional prototypes look like.

38
00:02:34,390 --> 00:02:43,000
Syntactically you notice that they're pretty much just the first line of the definition, right?

39
00:02:44,700 --> 00:02:49,020
So we have two right here and there for each one of these functions down here.

40
00:02:50,290 --> 00:02:55,720
So the first one, says Boyd, say hello with some parentheses, which is pretty much just line 12,

41
00:02:55,720 --> 00:03:02,950
right, is the first line of the function definition, the function implementation right here, right?

42
00:03:04,060 --> 00:03:06,280
So what we do is just for the prototype.

43
00:03:06,280 --> 00:03:11,500
We're going to take everything on this first line, but not including the curly brace, of course.

44
00:03:11,510 --> 00:03:17,160
Instead, we'll have a semicolon afterwards since it's the C++ statement, something that we're defining

45
00:03:17,160 --> 00:03:18,290
in C++ here.

46
00:03:18,310 --> 00:03:25,270
Every line, this kind of an actual line of code ends up having a semicolon after it to finish it off.

47
00:03:27,070 --> 00:03:29,760
So we have the one for say hello.

48
00:03:29,770 --> 00:03:31,750
We also have one for Find Some.

49
00:03:32,380 --> 00:03:43,480
If we want to declare these up here as prototypes, then we would run into those issues when we called

50
00:03:43,480 --> 00:03:47,320
say hello here in Maine and call to find some.

51
00:03:47,590 --> 00:03:51,640
What would actually happen is and say hello is the first thing that runs in Maine.

52
00:03:52,090 --> 00:03:56,450
If these didn't exist, then it would have a compiler error.

53
00:03:56,470 --> 00:04:01,320
Once it saw this, it would say, OK, well, this is not in scope.

54
00:04:01,330 --> 00:04:05,330
Say hello is not something recognized in my scope, right?

55
00:04:06,370 --> 00:04:11,440
If you got rid of say hello, but or sorry, if you got rid of find some but left say hello, then it

56
00:04:11,440 --> 00:04:13,510
would actually fail here on the find some.

57
00:04:14,970 --> 00:04:21,840
Another important thing is notice that the Find Some also has the parameters mentioned that is also

58
00:04:21,840 --> 00:04:26,280
necessary for the prototype if you have parameters in the function definition.

59
00:04:26,520 --> 00:04:28,410
You must also put them in the prototype.

60
00:04:28,890 --> 00:04:34,280
Later on, we might see that you don't necessarily have to include the very variable name here.

61
00:04:34,290 --> 00:04:41,100
You can just put the word end to signify that the first parameter is an integer and you could just put

62
00:04:41,100 --> 00:04:46,230
and for the second parameter or two to say, Hey, the second parameter is an integer, but I like to

63
00:04:46,230 --> 00:04:48,060
do it this way because it just makes sense.

64
00:04:48,060 --> 00:04:49,680
Like why not put the variable names here?

65
00:04:49,690 --> 00:04:53,190
Why not just like, line it up so you everything makes sense?

66
00:04:53,220 --> 00:04:53,670
I don't know.

67
00:04:53,700 --> 00:04:54,690
That's just my preference.

68
00:04:54,690 --> 00:04:54,960
But.

69
00:04:56,680 --> 00:05:01,960
Pretty simple, not much more to it, just, you know, everything on this first line is what's going

70
00:05:01,960 --> 00:05:04,540
to be the prototype and when do you do the prototype?

71
00:05:04,870 --> 00:05:10,560
You have to do the prototype if you're going to put these functions below wherever you call it.

72
00:05:10,570 --> 00:05:13,680
So that's the main thing you.

73
00:05:13,960 --> 00:05:16,840
From top to bottom, moving from top to bottom.

74
00:05:17,200 --> 00:05:26,110
You cannot call a function before it is defined unless you have a prototype.

75
00:05:27,120 --> 00:05:27,540
All right.

76
00:05:28,860 --> 00:05:32,610
You may be thinking that this seems like kind of a dumb thing, like why do we even care?

77
00:05:32,640 --> 00:05:34,320
Why can't we just put these above me?

78
00:05:34,350 --> 00:05:35,640
Well, yeah, you can.

79
00:05:35,880 --> 00:05:40,950
It's going to be more interesting, though later on we're going to use prototypes when we have multiple

80
00:05:40,950 --> 00:05:43,200
files from one program in C++.

81
00:05:44,510 --> 00:05:49,970
So you kind of see the purpose of prototypes at that point when we want to use multiple files, we have

82
00:05:49,970 --> 00:05:51,170
these things called header files.

83
00:05:51,170 --> 00:05:53,060
We're going to get into that later on.

84
00:05:54,940 --> 00:05:55,380
All right.

85
00:05:55,750 --> 00:05:58,360
So let's go ahead and move on to the next thing.

86
00:05:58,600 --> 00:06:04,600
So the next thing is simply just me trying to introduce functions, calling other functions.

87
00:06:05,380 --> 00:06:07,240
You notice I have some prototypes up here.

88
00:06:07,640 --> 00:06:08,980
Voight avoid be void.

89
00:06:08,980 --> 00:06:09,390
See?

90
00:06:10,560 --> 00:06:16,690
That's because these three functions are implemented below main and I call a here and main.

91
00:06:18,190 --> 00:06:20,050
So pretty interesting.

92
00:06:20,350 --> 00:06:28,180
We have a gets called right here, so in Maine, when the program runs was going to do is it's going

93
00:06:28,180 --> 00:06:33,400
to see a it's going to jump to the function a the implementation here, the definition.

94
00:06:34,980 --> 00:06:36,600
And there's going to run this line of code.

95
00:06:37,050 --> 00:06:42,900
Then it's going to be so it's going to jump to be around this line of code in a CCS can jump to see

96
00:06:42,900 --> 00:06:43,950
it or in this line of code.

97
00:06:45,150 --> 00:06:49,950
So let's go ahead and step in to the editor and kind of look at some things.

98
00:06:53,290 --> 00:06:59,200
So one thing that I want to point out right off the bat is what I was mentioning when you don't have

99
00:06:59,200 --> 00:07:03,760
a prototype, so I'm going to go ahead and comment this out so we can see what happens.

100
00:07:04,630 --> 00:07:05,590
I'll save this.

101
00:07:05,890 --> 00:07:08,140
I'll go ahead and compile it.

102
00:07:08,650 --> 00:07:10,900
Well, look, it looks like we have an error.

103
00:07:12,080 --> 00:07:15,470
So let's scroll up and see what this area is, looks.

104
00:07:18,200 --> 00:07:21,920
Go ahead and clear and compile again so we can get something cleaner.

105
00:07:23,510 --> 00:07:28,850
OK, so it says that we have an air online eight.

106
00:07:29,980 --> 00:07:31,150
Right here in Line eight.

107
00:07:32,080 --> 00:07:35,080
And it says, and of course, notice this is line eight.

108
00:07:35,530 --> 00:07:40,750
It says Error A was not declared in this scope, but that's what we were talking about since it wasn't

109
00:07:40,750 --> 00:07:43,240
prototyped up here when we get to Maine.

110
00:07:43,690 --> 00:07:45,890
Maine says I don't know what this is.

111
00:07:45,970 --> 00:07:47,310
I've never seen this.

112
00:07:47,320 --> 00:07:51,700
How can I call a function that I've never seen and I don't know where it is or whether it exists?

113
00:07:52,540 --> 00:07:54,400
That's pretty much what this error means.

114
00:07:55,730 --> 00:07:57,770
Right, so you ahead and uncomment this.

115
00:08:01,010 --> 00:08:03,290
Go ahead and save it, compile it.

116
00:08:06,030 --> 00:08:07,280
Right, there we go.

117
00:08:09,680 --> 00:08:15,410
And so it really matters like the fact that you just put it before the call, so let's go ahead and

118
00:08:15,410 --> 00:08:16,100
try this.

119
00:08:16,400 --> 00:08:20,210
So if I go down here, I delete that.

120
00:08:20,210 --> 00:08:20,960
I copied it.

121
00:08:21,440 --> 00:08:24,260
So what I can do is just put it right here.

122
00:08:26,570 --> 00:08:31,190
Let's go ahead and save this and a clear.

123
00:08:32,810 --> 00:08:37,940
You notice that this compiles fine, even though we didn't put the void B prototype up here with the

124
00:08:37,940 --> 00:08:39,530
other ones, we put it below main.

125
00:08:39,980 --> 00:08:40,850
Why is that?

126
00:08:41,180 --> 00:08:41,970
Why is it fine?

127
00:08:41,990 --> 00:08:44,240
Well, it's because we don't call be in Maine.

128
00:08:44,360 --> 00:08:45,680
We only call it a right.

129
00:08:45,710 --> 00:08:53,540
The rule is that you have to make sure to either have the definition before you call it or a prototype

130
00:08:53,540 --> 00:08:54,380
before you call it.

131
00:08:54,710 --> 00:09:00,830
Let's see what happens, though, when I move it below a you notice that Function A. calls Function

132
00:09:00,830 --> 00:09:09,020
B. So what if I don't prototype B until down here and we'll go ahead and save this and compile it?

133
00:09:11,160 --> 00:09:12,060
Look what happened.

134
00:09:12,840 --> 00:09:17,130
It says Line 14, you notice that's this line right here.

135
00:09:17,670 --> 00:09:20,190
It says Air B was not declared in this scope.

136
00:09:20,190 --> 00:09:23,650
So same thing a function a it's like, I don't know what this is.

137
00:09:23,670 --> 00:09:24,420
I've never seen this.

138
00:09:24,420 --> 00:09:26,940
How can I call this function that I don't know what it is?

139
00:09:28,080 --> 00:09:28,400
All right.

140
00:09:28,410 --> 00:09:30,390
So that's why we're going to go ahead.

141
00:09:30,390 --> 00:09:35,130
And I think the cleanest kind of way is to just put them on top here.

142
00:09:36,450 --> 00:09:40,710
So I like to go ahead and just put them in order up top here.

143
00:09:43,910 --> 00:09:45,320
So pretty cool.

144
00:09:45,980 --> 00:09:49,040
You notice I can go ahead and run this now.

145
00:09:49,820 --> 00:09:56,330
I'll compile it and run it, so I actually do this.

146
00:09:56,450 --> 00:10:01,400
So you notice that it says we're in function, a let's call a function B. It jumps to B.

147
00:10:01,790 --> 00:10:05,300
It says hello from function B, where c it jumps.

148
00:10:05,320 --> 00:10:08,990
It calls C and jumps to see it says, Hello, this is C right.

149
00:10:10,250 --> 00:10:12,020
We could, of course, change this around.

150
00:10:12,020 --> 00:10:14,020
You know, it doesn't have to be in this order.

151
00:10:14,030 --> 00:10:20,150
These functions could call each other in different orders like we can have this call Maine calls Function

152
00:10:20,150 --> 00:10:25,130
C, function C calls function a.

153
00:10:27,860 --> 00:10:35,360
Function calls, function B, and then of course, I would want to comment this out, and I'll show

154
00:10:35,360 --> 00:10:36,920
you why in a second.

155
00:10:40,210 --> 00:10:48,160
So I can go ahead and compile this and run it, so now it's in a different order, right?

156
00:10:48,910 --> 00:10:54,730
We start in Maine, we call Function C, so it says Hello, hello, I'm sorry right here, says hello,

157
00:10:54,730 --> 00:10:55,570
this is C.

158
00:10:56,550 --> 00:10:57,800
See, calls function a.

159
00:10:57,810 --> 00:11:04,980
So we jump up to function A and it says wear in function a let's call function B, so it cost function

160
00:11:04,980 --> 00:11:05,390
B.

161
00:11:05,730 --> 00:11:10,800
It goes to be in and says hello from function B where c but CRT got called up here.

162
00:11:11,190 --> 00:11:12,450
We just changed the ordering.

163
00:11:14,130 --> 00:11:22,380
But something interesting is what happens if we did not comment this or did not comment this out.

164
00:11:24,030 --> 00:11:24,600
So.

165
00:11:25,890 --> 00:11:30,450
Before I run this, I want you to take a look at this code here.

166
00:11:31,350 --> 00:11:32,640
We make some room.

167
00:11:34,210 --> 00:11:39,690
Want you to take a look at this code here and think about what is going to happen.

168
00:11:40,410 --> 00:11:40,800
All right.

169
00:11:41,460 --> 00:11:45,510
Trace the code, trace the function calls.

170
00:11:46,510 --> 00:11:52,450
We start out and see, so go ahead and think about starting out from see main cause see, so we get

171
00:11:52,450 --> 00:11:58,750
to see right what happens afterwards, though, you know, there's a call to a here, you know, there's

172
00:11:58,750 --> 00:11:59,910
a call to be here.

173
00:11:59,950 --> 00:12:01,840
You know, there's a call to see here.

174
00:12:03,730 --> 00:12:07,030
I want you to think about what's going to happen, and I will go ahead and show you.

175
00:12:08,740 --> 00:12:09,160
So.

176
00:12:10,470 --> 00:12:13,680
I think I already say this, but I'll just say it again, just just in case.

177
00:12:17,250 --> 00:12:21,960
All right, so let's compile this and let's run it.

178
00:12:22,890 --> 00:12:25,270
Oh, what is going on here?

179
00:12:25,290 --> 00:12:26,560
Oh my gosh.

180
00:12:27,240 --> 00:12:29,280
It's just printing so much stuff.

181
00:12:29,310 --> 00:12:31,680
This looks like an infinite wow loop, doesn't it?

182
00:12:32,520 --> 00:12:36,630
Don't worry, you can exit it out just the same way that you did with the infinite while loop with a

183
00:12:36,630 --> 00:12:37,620
control seal.

184
00:12:39,390 --> 00:12:42,160
So you control C, then it'll stop it.

185
00:12:42,680 --> 00:12:46,850
Well, look what it's doing it just as like, Hello, this is c one functional.

186
00:12:46,860 --> 00:12:47,830
It's called function B.

187
00:12:47,850 --> 00:12:48,180
Hello.

188
00:12:48,180 --> 00:12:50,100
This is function B where c hello.

189
00:12:50,100 --> 00:12:54,030
This is C, you know, and it goes right back to C.

190
00:12:55,640 --> 00:12:58,250
That's because it's just jumping back and forth.

191
00:12:58,280 --> 00:13:01,700
This is an interesting topic that is going to be covered.

192
00:13:02,510 --> 00:13:08,960
Quite a ways ahead in the course, in the data structures and algorithms section.

193
00:13:10,150 --> 00:13:11,770
It's something called recursion.

194
00:13:12,670 --> 00:13:21,220
It's a slightly more advanced topic, but all it really is is when a function either directly or indirectly

195
00:13:21,220 --> 00:13:26,170
calls itself, which is kind of what's happening, it's almost like making a circle and coming right

196
00:13:26,170 --> 00:13:27,370
back to where it started, right?

197
00:13:27,850 --> 00:13:34,760
We main calls C C calls a equals B, so let's just do that again.

198
00:13:34,780 --> 00:13:36,790
I'll try and have a little smoother, so make calls.

199
00:13:36,860 --> 00:13:39,550
See, we're seeing a we go to a.

200
00:13:41,190 --> 00:13:50,490
A Class B, we got to be because C we got a C C calls a we go to a AA Class B, we got a B because see

201
00:13:50,490 --> 00:13:55,680
we got a C C calls a we just keep doing that over and over again, just bouncing around between these.

202
00:13:56,490 --> 00:13:59,730
So that is something called indirect recursion.

203
00:14:00,750 --> 00:14:06,000
Direct recursion would be something like this when C just calls itself and it just keeps going like

204
00:14:06,000 --> 00:14:06,270
this.

205
00:14:06,270 --> 00:14:10,080
It just as like, call myself, go to hear myself, go to here, call myself going to here, and it

206
00:14:10,080 --> 00:14:14,730
would just get stuck printing, hello, this is C for a super long time.

207
00:14:16,830 --> 00:14:18,030
So we can see that.

208
00:14:19,040 --> 00:14:26,660
If I compile this, this looks a little wonky, but you know, this is just doing hello to see over

209
00:14:26,660 --> 00:14:27,590
and over and over again.

210
00:14:28,760 --> 00:14:32,620
And you could just scroll up and it would just be like you could see me scrolling over here.

211
00:14:32,630 --> 00:14:34,340
There's a lot of stuff that printed out.

212
00:14:36,110 --> 00:14:37,460
So of course we don't.

213
00:14:37,790 --> 00:14:38,610
We don't want that.

214
00:14:38,630 --> 00:14:41,360
You know, this is something that.

215
00:14:42,590 --> 00:14:44,000
We're not going to go over at this point.

216
00:14:44,210 --> 00:14:49,460
Just wanted to show you because their chances are that you might have made a mistake and you have an

217
00:14:49,460 --> 00:14:54,500
indirect or direct recursion in here, or you might have just messed around and you saw that happen

218
00:14:54,500 --> 00:14:55,970
and you were like, Well, this is crazy.

219
00:14:55,970 --> 00:14:57,230
It looks like an infinite loop.

220
00:14:57,770 --> 00:15:00,020
So I wanted to mention that just in case.

221
00:15:02,330 --> 00:15:02,930
All right.

222
00:15:03,230 --> 00:15:09,080
So I just wanted to demonstrate that you can, of course, use multiple functions and the functions

223
00:15:09,080 --> 00:15:12,150
can call themselves and not call themselves sorry.

224
00:15:12,170 --> 00:15:14,930
The functions can call each other and stuff like that.

225
00:15:15,410 --> 00:15:18,260
You could actually put multiple calls here if you'd like to.

226
00:15:18,740 --> 00:15:21,290
You don't just have to have it call one each.

227
00:15:22,550 --> 00:15:27,260
You can call function a from Maine and then.

228
00:15:28,520 --> 00:15:34,820
You could actually call function B and C from A if you wanted, and you could do the same with these

229
00:15:34,820 --> 00:15:35,780
other functions to.

230
00:15:37,680 --> 00:15:38,090
Right.

231
00:15:38,130 --> 00:15:41,430
So pretty cool functions are really powerful.

232
00:15:42,280 --> 00:15:49,740
Main thing to take away from this is the prototypes in relation to where you place the definition in

233
00:15:49,740 --> 00:15:51,180
relation to the calls.

234
00:15:51,240 --> 00:15:52,260
All that stuff, right?

235
00:15:52,890 --> 00:15:58,440
However, how they the position of the call in the program in the file compared to the position of the

236
00:15:58,440 --> 00:16:01,260
definition that is really what the prototype is based on, right?

237
00:16:01,710 --> 00:16:07,080
We need to make sure that we have either the definition of the function, which is also the implementation.

238
00:16:07,080 --> 00:16:12,000
You know, this is the implementation of the function when you make sure that we have that before the

239
00:16:12,000 --> 00:16:12,730
function call.

240
00:16:12,750 --> 00:16:15,180
Otherwise, we have to do these prototypes of you.

241
00:16:16,590 --> 00:16:17,000
All right.

242
00:16:17,010 --> 00:16:22,380
So with that, I think we are pretty much done with our introduction to functions now.

243
00:16:23,130 --> 00:16:30,900
Later on, we will be deep diving more into functions, but it's going to be in relation to other topics

244
00:16:30,900 --> 00:16:32,280
that we have not yet covered.

245
00:16:32,310 --> 00:16:33,660
That's why we have to hold off.

246
00:16:35,010 --> 00:16:37,380
So with that, I will see you in the next lectures.
