1
00:00:00,120 --> 00:00:00,930
All right.

2
00:00:01,740 --> 00:00:09,120
The long awaited moment when we finally dive into functions.

3
00:00:10,170 --> 00:00:15,900
I apologize for the wait for so long, you've probably been like, Oh my gosh, he keeps introducing

4
00:00:15,900 --> 00:00:20,790
all these new quote unquote functions that we're using.

5
00:00:20,850 --> 00:00:28,740
Yet we still haven't really had a specific lecture on what function is and how we could make our own

6
00:00:28,740 --> 00:00:30,010
functions and stuff like that.

7
00:00:30,030 --> 00:00:31,230
Well, no longer.

8
00:00:31,410 --> 00:00:33,840
We now are on that topic.

9
00:00:34,500 --> 00:00:39,030
It's going to probably take a few lectures to cover it because it's a big topic.

10
00:00:40,020 --> 00:00:41,430
I really want to.

11
00:00:42,770 --> 00:00:53,030
Make sure everyone understands because it is a topic that has kind of a steep learning curve, maybe

12
00:00:53,030 --> 00:01:00,980
for certain people to understand what it really is and what happens when that, like our function gets

13
00:01:00,980 --> 00:01:02,840
used in your code, was the computer do.

14
00:01:03,470 --> 00:01:07,820
So we'll have that part where we talk about what's really happening behind the scenes.

15
00:01:07,820 --> 00:01:12,020
And then in this lecture, we're going to kind of go over the basics of what a function is and get into,

16
00:01:12,020 --> 00:01:16,640
like how we code the functions and stuff like that and talk about all the parts.

17
00:01:17,690 --> 00:01:20,360
So let's finally get started with this.

18
00:01:21,680 --> 00:01:25,610
So first off, I'm going to preface it with talking about some programming styles.

19
00:01:26,330 --> 00:01:31,940
So what we've been doing so far is something probably referred to as procedural programming, and this

20
00:01:31,940 --> 00:01:37,010
is something that I'm sure some people on StackOverflow can argue about.

21
00:01:38,510 --> 00:01:42,140
But it kind of is is what we've been doing.

22
00:01:42,230 --> 00:01:46,340
I haven't really mentioned it before, but we're programming right?

23
00:01:46,940 --> 00:01:52,940
And there's a few different types of programming which are kind of like programming styles.

24
00:01:53,960 --> 00:01:59,180
But what we've been doing is making programs that, of course, use control structures that the loops

25
00:01:59,180 --> 00:02:03,650
and the conditional, if else, if and else kind of stuff.

26
00:02:04,880 --> 00:02:10,250
And we've included some logic with brilliance and stuff, and we've been following sort of a top down

27
00:02:10,250 --> 00:02:10,910
approach.

28
00:02:11,330 --> 00:02:11,720
Right.

29
00:02:12,050 --> 00:02:17,220
So we kind of have our main function and we start at the top and then we just do stuff in order, right?

30
00:02:17,240 --> 00:02:19,340
We make some variables at the top.

31
00:02:20,480 --> 00:02:26,360
Then we like do some calculations, maybe we have a loop, maybe we have some conditional statements

32
00:02:26,900 --> 00:02:32,330
and then once we have our results, we kind of like, you see out and we print them to the console.

33
00:02:33,920 --> 00:02:41,450
So we're going to continue on this path of procedural programming for a while, but we're going to introduce

34
00:02:41,450 --> 00:02:47,720
a kind of sub I would I would consider it like a sub style of that, which is modular programming.

35
00:02:48,650 --> 00:02:55,940
And so rather than just like following the top down approach, we're going to do something where we

36
00:02:55,940 --> 00:02:58,730
break the program into different sections.

37
00:02:58,730 --> 00:03:02,270
And this is kind of part of procedural programming you'll see in the definition.

38
00:03:03,140 --> 00:03:09,140
If you look that at it, it does talk about breaking down a routine and do some routines and stuff like

39
00:03:09,140 --> 00:03:09,410
that.

40
00:03:09,410 --> 00:03:13,400
A routine may be considered like the whole program.

41
00:03:13,670 --> 00:03:17,030
The program is like a process and you have some sub processes.

42
00:03:17,930 --> 00:03:23,330
You know, subroutines could be considered functions and stuff like that as well.

43
00:03:23,330 --> 00:03:24,130
And that's what we're going to do.

44
00:03:24,140 --> 00:03:30,080
We're going to introduce this topic of functions where we can kind of break up the work that needs to

45
00:03:30,080 --> 00:03:33,230
be done rather than just only using the main function.

46
00:03:35,240 --> 00:03:41,120
So we've been using functions, yes, and I've been talking about them, things like get line that is

47
00:03:41,120 --> 00:03:48,110
a function pretty much when you see a word that is a key word and it has these parentheses after it.

48
00:03:48,800 --> 00:03:50,120
That is a function call.

49
00:03:50,540 --> 00:03:55,670
So we've been using functions and we've been calling functions, but now it is time to make our own

50
00:03:55,670 --> 00:03:58,550
functions and really try and understand them better.

51
00:03:59,740 --> 00:04:00,220
So.

52
00:04:01,190 --> 00:04:07,820
We've been just using the main function, that's the function that we've been writing ourselves each

53
00:04:07,820 --> 00:04:08,450
time, right?

54
00:04:08,510 --> 00:04:15,800
We've kind of put into Maine and then some parentheses and some curly braces and inside the curly braces.

55
00:04:15,800 --> 00:04:19,220
We've been doing all of our program basically, right?

56
00:04:20,510 --> 00:04:23,990
So let's say that we don't want to have to write everything in Maine.

57
00:04:23,990 --> 00:04:26,750
We want to have some of that stuff in between.

58
00:04:26,750 --> 00:04:32,000
The curly braces in Maine be accomplished elsewhere in the program.

59
00:04:32,240 --> 00:04:37,400
So we want like another version of Maine kind of write another function that can handle some of the

60
00:04:37,400 --> 00:04:37,730
work.

61
00:04:38,150 --> 00:04:42,560
So in this example, you know, let's say Maine says, all right, function one, you do some stuff

62
00:04:42,890 --> 00:04:45,080
function to you do some other stuff that way.

63
00:04:45,080 --> 00:04:46,610
I don't have to handle all the workload.

64
00:04:48,950 --> 00:04:51,200
So what do they really do and what is their purpose?

65
00:04:51,230 --> 00:04:57,200
Well, the purpose of functions is that is to divide up the work your program means to do so.

66
00:04:57,200 --> 00:05:03,050
Just like the main function, we can make some other functions so Maine doesn't have to handle all of

67
00:05:03,050 --> 00:05:03,950
the work.

68
00:05:04,340 --> 00:05:08,720
We would perform some specific tasks with these functions.

69
00:05:08,960 --> 00:05:10,070
That's what the functions would do.

70
00:05:10,070 --> 00:05:16,220
They would be assigned like a specific task to do, and Maine would call that function and be like,

71
00:05:16,220 --> 00:05:17,810
Hey, you do this for me and function.

72
00:05:17,810 --> 00:05:18,920
One would do that task.

73
00:05:22,230 --> 00:05:29,880
So another huge benefit of functions is when it arises, when you need to perform the same type of task

74
00:05:30,510 --> 00:05:34,380
multiple times over and over, but maybe on different data.

75
00:05:35,100 --> 00:05:42,690
You don't want to have to rewrite the same code over and over again when you could just have the function

76
00:05:42,690 --> 00:05:43,080
do that.

77
00:05:43,080 --> 00:05:47,670
And each time you want that stuff done, you could just call the function.

78
00:05:47,670 --> 00:05:51,210
You could hit it up and be like, All right, please do this for me again.

79
00:05:51,870 --> 00:05:53,640
So that's what we're going to see in a moment.

80
00:05:55,270 --> 00:05:58,660
So we should revisit some of the technical aspects of functions.

81
00:05:59,110 --> 00:06:05,260
These are things that I mentioned a while back in another lecture, but I kind of want to refresh and

82
00:06:05,260 --> 00:06:06,910
then dive a little deeper into it.

83
00:06:06,940 --> 00:06:11,950
So one thing we talked about was that functions have return types.

84
00:06:12,640 --> 00:06:15,850
So function is like, I'm going to do stuff in here.

85
00:06:15,860 --> 00:06:17,470
This box is a function.

86
00:06:17,920 --> 00:06:25,480
After it does stuff, then it's going to maybe pop something out, and that is going to be a certain

87
00:06:25,480 --> 00:06:26,320
data type, right?

88
00:06:26,340 --> 00:06:31,300
It's going to do stuff with some data and then put out some data, potentially.

89
00:06:32,980 --> 00:06:40,540
There always is going to be a return type that you must declare for the function, and that can be an

90
00:06:40,540 --> 00:06:47,350
end, a float, a string or something called void, which is basically nothing.

91
00:06:47,830 --> 00:06:55,300
That's just saying like, we don't have a return type, but you still need to like clearly state that

92
00:06:55,350 --> 00:06:56,620
the function is void.

93
00:06:56,650 --> 00:06:58,570
You can't just not put a return type.

94
00:06:58,570 --> 00:07:03,820
You got to put some sort of return type, even if it is like a do nothing return, nothing type.

95
00:07:04,630 --> 00:07:08,170
There's also much more as we'll see as we get further into the course.

96
00:07:08,590 --> 00:07:13,900
We're not limited by these primitive types or even some of the other types that we've discussed, like

97
00:07:13,900 --> 00:07:14,550
string.

98
00:07:14,560 --> 00:07:19,630
We are not limited at all by what we can return from a function, pretty much.

99
00:07:20,140 --> 00:07:24,400
So we'll just see more and more types and we'll well later on talk about creating our own types.

100
00:07:25,450 --> 00:07:32,980
So this is just about you understanding that functions return stuff potentially, but must always not

101
00:07:32,980 --> 00:07:38,710
potentially must always have a defined return type, whether it returns something or not.

102
00:07:38,950 --> 00:07:41,080
You must define a return type and void.

103
00:07:41,080 --> 00:07:43,060
It would be if it does not return anything.

104
00:07:43,060 --> 00:07:47,860
You still must explicitly say this function is void for the return type.

105
00:07:49,070 --> 00:07:54,560
Another thing is that they can receive data so they can have in-flight streaming and more, of course,

106
00:07:54,560 --> 00:07:56,630
because there's other data types we haven't discussed.

107
00:07:57,710 --> 00:08:04,520
That stuff can go into the function, so the box is still the function and stuff comes in and the functions

108
00:08:04,520 --> 00:08:06,950
like I will do stuff with the data that I received.

109
00:08:09,390 --> 00:08:11,680
And it doesn't always have to receive data.

110
00:08:11,700 --> 00:08:14,900
This is like it can potentially receive data if you would like it to.

111
00:08:14,940 --> 00:08:21,000
Unlike the return type, which it may or may not return, it's something, but it must be defined what

112
00:08:21,000 --> 00:08:22,350
the return type is.

113
00:08:25,730 --> 00:08:34,430
OK, so let's let's let's take a look at what defining a function is versus calling a function.

114
00:08:34,440 --> 00:08:38,000
So there's two distinct kind of phases of functions.

115
00:08:38,720 --> 00:08:42,650
One is where you define what the function does.

116
00:08:43,100 --> 00:08:43,410
Right?

117
00:08:43,430 --> 00:08:46,970
You need to actually put a description of what's going on with the function.

118
00:08:46,970 --> 00:08:49,010
So that would be the function definition.

119
00:08:49,040 --> 00:08:50,090
You see the arrow here.

120
00:08:50,420 --> 00:08:54,170
This whole section here is the function definition.

121
00:08:54,170 --> 00:08:56,150
So to circle, it would be like this.

122
00:08:58,340 --> 00:09:01,250
This is the function definition.

123
00:09:01,550 --> 00:09:02,360
All this stuff here.

124
00:09:03,630 --> 00:09:07,800
So down here, this arrow is the function call.

125
00:09:08,550 --> 00:09:11,190
So this is a saying what the function does.

126
00:09:12,650 --> 00:09:19,580
And it's actually putting the code that will be ran when we call the function, and this is us calling

127
00:09:19,580 --> 00:09:25,880
the function because we want the function to do something for us in this very basic example.

128
00:09:26,240 --> 00:09:28,580
What happens is that it's a void function.

129
00:09:28,580 --> 00:09:32,440
It does not return anything, but it does something right.

130
00:09:32,450 --> 00:09:35,060
It prints out hello from my function.

131
00:09:35,660 --> 00:09:38,210
So you see that down here on the screen.

132
00:09:38,210 --> 00:09:43,630
And don't worry, we are going to break down these programs so you understand what's going on here more.

133
00:09:43,920 --> 00:09:49,760
But right now, this slide is just to tell you that there's two distinct parts the definition and where

134
00:09:49,760 --> 00:09:50,750
you call the function.

135
00:09:53,060 --> 00:09:55,800
So let's talk about the definition and break it down.

136
00:09:55,820 --> 00:09:56,840
What is this right here?

137
00:09:56,870 --> 00:10:00,770
Well, let's start at left and right and then go into it.

138
00:10:02,250 --> 00:10:08,550
So right here, this is the return type, and we talk about a void just as this function does not return

139
00:10:08,550 --> 00:10:09,150
anything.

140
00:10:09,180 --> 00:10:16,920
But like I said, we must explicitly define that we must say this function returns nothing.

141
00:10:18,000 --> 00:10:19,860
So that's why we say void.

142
00:10:20,280 --> 00:10:25,050
Just like down here with Maine, we've always been writing and Maine, right?

143
00:10:25,230 --> 00:10:26,160
And why is that?

144
00:10:26,640 --> 00:10:28,680
Because the return type is an end.

145
00:10:28,710 --> 00:10:29,640
Look right here.

146
00:10:30,540 --> 00:10:32,580
It says return zero.

147
00:10:32,580 --> 00:10:37,020
And we've talked about this a little bit, and this is the integer that gets returned to the operating

148
00:10:37,020 --> 00:10:40,170
system after the program finishes.

149
00:10:41,580 --> 00:10:47,820
So just like Maine, we also have to put a return type here, we have said in this example that there

150
00:10:47,820 --> 00:10:50,050
is no return type, so we put void.

151
00:10:51,630 --> 00:10:52,650
Let's move to the right.

152
00:10:53,220 --> 00:10:56,140
This right here is the name of the function.

153
00:10:56,160 --> 00:10:59,100
Yes, we get to name our own functions now.

154
00:10:59,100 --> 00:11:04,870
We do not have to put a specific name like Maine anymore.

155
00:11:04,890 --> 00:11:09,690
Maine is a special name that is known by the programming language, right?

156
00:11:09,690 --> 00:11:17,580
The compiler sees this and it's like, Oh, Maine, I know that this is us defining our own function

157
00:11:17,580 --> 00:11:20,820
name right here, and we're calling it my function.

158
00:11:22,240 --> 00:11:27,730
This is also a slight style guideline example here that we'll talk more about later, but you notice

159
00:11:27,730 --> 00:11:31,390
that it says my and then there's basically is two words.

160
00:11:31,540 --> 00:11:35,200
It's my and function, but we smash them together for the name.

161
00:11:36,400 --> 00:11:39,150
And the second word has starts with a capital letter.

162
00:11:39,160 --> 00:11:47,650
This style is something called camel case, and it's kind of the de facto styling for naming conventions

163
00:11:47,650 --> 00:11:51,400
and for functions, at least in C++.

164
00:11:51,400 --> 00:11:56,980
We'll notice that we can use Camel Case for different things in programming, and we'll talk more about

165
00:11:56,980 --> 00:12:00,160
C++ style and stuff like that.

166
00:12:00,160 --> 00:12:04,840
Different programming languages have different style guidelines, but that's something for later on,

167
00:12:04,850 --> 00:12:08,050
not quite as important as learning how to program in general.

168
00:12:08,980 --> 00:12:11,950
So the blue part is function is the function name.

169
00:12:12,100 --> 00:12:14,110
Then we have some parentheses here.

170
00:12:15,540 --> 00:12:18,480
And these parentheses are empty right now.

171
00:12:18,690 --> 00:12:27,630
But remember, we talked about the fact that if it is possible to have some input data going into the

172
00:12:27,630 --> 00:12:28,680
function, right?

173
00:12:30,010 --> 00:12:33,970
So it can take some data in instead of just returned data out.

174
00:12:34,600 --> 00:12:35,650
It can do both.

175
00:12:36,130 --> 00:12:41,200
It can do, you know, neither or it can do one or the other or something like that.

176
00:12:41,470 --> 00:12:41,920
So.

177
00:12:43,010 --> 00:12:48,950
These things are called parameters, if there's anything inside the parentheses, here it is, the functions

178
00:12:48,950 --> 00:12:52,880
input data where we probably want it to do stuff with that data.

179
00:12:53,940 --> 00:12:59,950
And the name for that is parameters, so multiple things can be inside here.

180
00:13:00,660 --> 00:13:01,860
Or just one thing?

181
00:13:02,460 --> 00:13:04,050
Those are called parameters.

182
00:13:05,870 --> 00:13:14,150
Then in between curly braces, that is the function body, just like the function body of Maine, is

183
00:13:14,180 --> 00:13:16,400
all of this stuff inside the curly braces.

184
00:13:18,280 --> 00:13:26,860
It's just like kind of inside, and if right or inside a loop, there's curly braces and stuff that

185
00:13:26,860 --> 00:13:30,160
happens inside the loops inside the curly braces, right?

186
00:13:30,520 --> 00:13:32,270
That's kind of like the body of the loop.

187
00:13:32,290 --> 00:13:34,570
You could think of this is the body of the function.

188
00:13:35,050 --> 00:13:37,390
All the stuff that happens inside the function.

189
00:13:40,000 --> 00:13:42,730
So those are the four main parts of the definition.

190
00:13:43,000 --> 00:13:44,170
Let's continue on.

191
00:13:45,730 --> 00:13:47,680
So this is the function call.

192
00:13:47,890 --> 00:13:49,710
You've noticed my arrows kind of overlapping.

193
00:13:49,720 --> 00:13:50,410
Sorry about that.

194
00:13:51,580 --> 00:13:58,030
But inside Maine, we are calling our function by using the function name.

195
00:13:59,080 --> 00:14:05,170
So we have my function that is this name of the function, right and the definition and then we have

196
00:14:05,170 --> 00:14:13,900
some empty parentheses and a semicolon after the parentheses are empty because we never defined any

197
00:14:13,900 --> 00:14:15,300
parameters up here, right?

198
00:14:15,310 --> 00:14:18,940
There's nothing inside of these parentheses in the definitions zone.

199
00:14:19,750 --> 00:14:24,290
If there were, then we would also need to put some stuff in the parentheses here.

200
00:14:24,310 --> 00:14:31,030
We did, in fact, not just need to put some stuff, we need to put the exact number of parameters as

201
00:14:31,030 --> 00:14:32,220
we're in the definition.

202
00:14:32,800 --> 00:14:38,050
But down here and we will see what this really looks like in a moment when I give you an example with

203
00:14:38,050 --> 00:14:38,650
parameters.

204
00:14:39,190 --> 00:14:47,030
But when you put the input data in the function, call for it to get sent up to the function definition.

205
00:14:47,950 --> 00:14:50,350
It's called arguements when it's down here.

206
00:14:50,500 --> 00:14:57,820
So definition, we call it parameters for the stuff in between the parentheses or the function call.

207
00:14:57,830 --> 00:14:59,440
We call it arguments.

208
00:14:59,530 --> 00:15:02,710
So they're both function input data you see, right?

209
00:15:03,890 --> 00:15:06,800
But we call it argument somewhere down here in the call.

210
00:15:09,960 --> 00:15:15,630
So let's take a look at that where we have parameters and not only parameters, but rather than having

211
00:15:15,630 --> 00:15:20,490
a void return type, we actually have an integer return type just like main.

212
00:15:21,570 --> 00:15:29,010
So let's go ahead and break this down, so I'm going to talk about the call first because we're going

213
00:15:29,010 --> 00:15:31,110
to act like we're starting out in Maine.

214
00:15:31,890 --> 00:15:34,890
So what's happening is that?

215
00:15:35,930 --> 00:15:38,900
The main function gets called when the program is run, right?

216
00:15:39,740 --> 00:15:43,460
So we have this at numbers right here.

217
00:15:43,490 --> 00:15:44,930
This is our function name.

218
00:15:45,910 --> 00:15:51,610
What we're doing is we are passing the functions, so you can imagine this like some kind of game with

219
00:15:51,610 --> 00:15:52,450
a ball, right?

220
00:15:53,500 --> 00:15:59,890
Imagine that we have, you know, kind of multiple it's like a football or something you can think of

221
00:15:59,890 --> 00:16:06,640
like that, whether you're talking about football, like in the European sense, like soccer or American

222
00:16:06,640 --> 00:16:07,810
football either or.

223
00:16:08,720 --> 00:16:15,860
But let's just say that we have, you know, two footballs right here, and but somehow one person is

224
00:16:15,860 --> 00:16:19,120
going to pass both to this other player, right?

225
00:16:20,980 --> 00:16:28,210
So Maine is like saying, OK, I'm going to pass to I'm going to pass this input data.

226
00:16:29,250 --> 00:16:30,630
Over to this other player.

227
00:16:32,140 --> 00:16:37,000
So we have these arguments here, which is the phone function input data, which is kind of like the

228
00:16:37,000 --> 00:16:38,890
ball that you're passing, right?

229
00:16:38,890 --> 00:16:43,450
The ball has multiple things or you can think of it as two footballs or something like that.

230
00:16:44,720 --> 00:16:50,300
We take it and we send it up here and you notice this is going to go here.

231
00:16:51,290 --> 00:16:55,640
And this is going to go here, that's because they are positional arguments.

232
00:16:56,480 --> 00:16:56,930
All right.

233
00:16:57,230 --> 00:17:00,740
Five is the first thing in the parentheses.

234
00:17:01,100 --> 00:17:03,710
Three is the second thing in the parentheses.

235
00:17:04,720 --> 00:17:11,740
So that means that five will be right here when we pass it, the five gets passed up here to number

236
00:17:11,740 --> 00:17:12,130
one.

237
00:17:13,260 --> 00:17:20,070
It's just like assigning it to a variable like imagine we had and no one down here in Maine and we said,

238
00:17:20,070 --> 00:17:22,350
and no one equals five.

239
00:17:22,710 --> 00:17:27,990
That's kind of what's happening here, except it's happening automatically when we put a five right

240
00:17:27,990 --> 00:17:28,410
here.

241
00:17:28,710 --> 00:17:33,300
The compiler sees this and it's going to add numbers and it says, Oh, OK.

242
00:17:34,430 --> 00:17:40,220
Number one, it contains the value five, because five was an argument, in fact, it was the first

243
00:17:40,220 --> 00:17:44,630
argument, so the first parameter is going to hold the value five.

244
00:17:46,270 --> 00:17:51,640
Notice there's a comma separating both of them, so this is the first one, then we have a comma, then

245
00:17:51,640 --> 00:17:58,030
we have the second parameter and down here, just like the second parameter, we have the second argument,

246
00:17:58,030 --> 00:17:58,420
right?

247
00:17:59,080 --> 00:18:00,760
Three is the second argument.

248
00:18:00,760 --> 00:18:05,380
So just like you might have thought, three is getting saved in two.

249
00:18:07,190 --> 00:18:09,890
So now let's jump up to here and talk about this.

250
00:18:10,580 --> 00:18:16,490
Number one contains five and two contains three because we called this function, add numbers right.

251
00:18:18,060 --> 00:18:22,240
We can now refer to number one and get its value right.

252
00:18:22,260 --> 00:18:29,010
We're allowed to use it because the function has knowledge of the parameters here.

253
00:18:29,310 --> 00:18:32,790
It understands what these parameters are.

254
00:18:32,940 --> 00:18:34,760
It recognizes the names.

255
00:18:34,770 --> 00:18:39,930
So we are allowed to do things inside the body of the function with number one and number two.

256
00:18:40,290 --> 00:18:43,020
We're only doing one thing, though on one line.

257
00:18:43,560 --> 00:18:48,660
And what we're doing is we are returning number one plus numb to.

258
00:18:49,740 --> 00:18:55,500
So you don't necessarily just have to return one single number, like the return of zero here, you

259
00:18:55,500 --> 00:19:00,870
can also return an expression that is an integer as well, right?

260
00:19:00,880 --> 00:19:06,510
So if you have five plus three, that is eight right, five plus three is eight.

261
00:19:07,110 --> 00:19:12,190
Imagine that now what this boils down to is like return eight.

262
00:19:12,250 --> 00:19:14,010
So you see this says returns zero.

263
00:19:14,220 --> 00:19:20,040
We're saying return five plus three, which is number one and number two, that's what their values

264
00:19:20,040 --> 00:19:21,660
are inside of these variables.

265
00:19:22,230 --> 00:19:27,540
So it's essentially the same thing as saying Return eight, which is kind of related to this down here,

266
00:19:27,540 --> 00:19:27,750
right?

267
00:19:27,750 --> 00:19:28,680
Returns zero.

268
00:19:28,680 --> 00:19:29,940
It's kind of a similar thing.

269
00:19:31,050 --> 00:19:37,680
So we return eight and it basically passes the ball back to here where we left off, right?

270
00:19:37,710 --> 00:19:38,790
It's like playing catch.

271
00:19:39,940 --> 00:19:45,280
Mane said Here, let me pass you some data, pass the data up the function, add numbers, say like,

272
00:19:45,280 --> 00:19:49,330
Hey, let me do something with this data, I'm going to add it together and give you something back.

273
00:19:49,630 --> 00:19:51,460
So it added five plus three.

274
00:19:51,460 --> 00:19:52,030
That was eight.

275
00:19:52,030 --> 00:19:55,420
And it says, Hey, hey, yo, man, I'm going to pass eight back to you.

276
00:19:55,810 --> 00:19:58,990
And so Mane catches it right here where it left off.

277
00:19:59,500 --> 00:20:01,420
It passed two things up here.

278
00:20:01,810 --> 00:20:04,390
This added them, and it passed one thing back.

279
00:20:04,390 --> 00:20:06,280
And now just eight is right here.

280
00:20:07,240 --> 00:20:11,860
You've seen something like this before a variable with an assignment operator.

281
00:20:12,430 --> 00:20:16,030
Now imagine there's the number eight right here.

282
00:20:17,220 --> 00:20:22,110
So what happens is that eight gets assigned to this variable now.

283
00:20:23,230 --> 00:20:25,630
So you save the result to a variable.

284
00:20:27,550 --> 00:20:33,340
So that's what happened, what the compiler did first or actually when the program was running, I should

285
00:20:33,340 --> 00:20:33,760
say.

286
00:20:34,960 --> 00:20:39,590
Is that ad numbers got called and it got sent data.

287
00:20:39,610 --> 00:20:46,120
It did some stuff, returned it back to here, and then we did the part where we assigned that result

288
00:20:46,120 --> 00:20:51,950
from the return to this variable called result, right?

289
00:20:51,970 --> 00:20:57,550
It's an integer variable and eight is what we got back and eight got saved into here.

290
00:20:57,560 --> 00:20:58,630
It's equal sign.

291
00:20:58,630 --> 00:20:59,530
It gets saved in here.

292
00:21:00,160 --> 00:21:04,420
Finally, we print that out and you notice that in the console we see the number eight.

293
00:21:07,660 --> 00:21:13,180
So let's walk through this previous example again, just to really break it down, because it's kind

294
00:21:13,180 --> 00:21:13,990
of complicated.

295
00:21:15,580 --> 00:21:16,780
We start here.

296
00:21:18,200 --> 00:21:22,610
I'm going to break it down step by step with the arrows so you can see what's going on rather than me

297
00:21:22,610 --> 00:21:23,840
just dragging my pointer around.

298
00:21:25,130 --> 00:21:33,080
So we start out in the program when the program runs, it goes to Maine, so we go inside of Maine.

299
00:21:35,300 --> 00:21:37,460
Then we see this line here on Line Nine.

300
00:21:37,490 --> 00:21:39,650
It says Entwistle equals add numbers.

301
00:21:41,430 --> 00:21:45,990
Rather than doing this right away, we kind of push that off to the side.

302
00:21:46,020 --> 00:21:50,070
What we do first is we go to the function call because we need to handle this.

303
00:21:50,670 --> 00:21:54,390
So we see, oh, add numbers, we're going to pass it some data.

304
00:21:54,390 --> 00:22:01,050
So we go up here, we pass it, five here and three here.

305
00:22:01,350 --> 00:22:07,650
So the variable of type integer number one contains five.

306
00:22:08,070 --> 00:22:11,880
The variable of type integer known contains three.

307
00:22:13,530 --> 00:22:17,490
We now go inside the add numbers function definition to the body.

308
00:22:18,000 --> 00:22:19,920
We are now in the body of the function.

309
00:22:21,510 --> 00:22:26,520
The body only has one line, it says return number one plus tip number two.

310
00:22:27,210 --> 00:22:31,170
So what we do is we evaluate number one plus number two.

311
00:22:32,540 --> 00:22:35,990
No one plus two is five plus three.

312
00:22:36,650 --> 00:22:38,330
That value is eight.

313
00:22:38,990 --> 00:22:42,560
Now we look at the fact that we must return eight.

314
00:22:43,160 --> 00:22:44,440
So that's what we do.

315
00:22:44,450 --> 00:22:48,410
We return eight and we pass it back to this area right here.

316
00:22:49,490 --> 00:22:52,430
We have now come back to this line.

317
00:22:53,120 --> 00:23:01,970
This area over here has the value eight, which gets now saved with this assignment operator into a

318
00:23:02,060 --> 00:23:10,790
new variable, a new variable integer result, type integer and it now health evaluate.

319
00:23:11,870 --> 00:23:19,970
We then had to line 10 and we noticed that we must see out whatever is inside of the variable result

320
00:23:20,360 --> 00:23:22,100
and then must have an in line.

321
00:23:23,030 --> 00:23:24,290
And that is what we do.

322
00:23:24,560 --> 00:23:26,570
So that prints out to the console.

323
00:23:26,870 --> 00:23:28,240
We see the value eight.

324
00:23:29,780 --> 00:23:36,950
Now we hit the bottom of main and it says return zero back to the operating system and the program finishes.
