1
00:00:00,360 --> 00:00:06,270
An interesting thing is that we can pass a raise to functions as well.

2
00:00:06,840 --> 00:00:10,170
So if you did this problem, you might have some of you.

3
00:00:10,200 --> 00:00:16,260
I'm not expecting it, but some of you might have thought, Hmm, I want to read this into an array,

4
00:00:16,800 --> 00:00:20,580
then I want to send the array of words to the function.

5
00:00:20,910 --> 00:00:21,780
You might have done that.

6
00:00:21,780 --> 00:00:26,190
You might not, but I'm guessing that you had to probably look something up for that, right?

7
00:00:26,400 --> 00:00:32,340
Unless you already knew how to do that in C++, you probably had to go online and see how you can pass

8
00:00:32,340 --> 00:00:36,030
an array to a function or even if you can, which you can.

9
00:00:36,360 --> 00:00:39,240
So let's head to the editor to see what that looks like.

10
00:00:39,510 --> 00:00:42,150
For those of you that haven't done that and it didn't look that up.

11
00:00:43,930 --> 00:00:45,250
So we're going to go over here.

12
00:00:47,140 --> 00:00:50,170
So I have a different program here.

13
00:00:50,470 --> 00:00:59,230
This program actually just calculates the sum of all the integers in an array, so it gets it from the

14
00:00:59,230 --> 00:01:01,010
user, it gets three integers.

15
00:01:01,540 --> 00:01:06,100
So right here we have an array of integers size three called nums.

16
00:01:06,430 --> 00:01:11,170
We tell the user, please enter three numbers separated by spaces, all in one line.

17
00:01:11,170 --> 00:01:14,320
We see into each one of the positions of the array.

18
00:01:14,590 --> 00:01:18,640
Since the array is size three, it has indices zero, one and two.

19
00:01:20,390 --> 00:01:25,940
You could just hard code the size to three, since you know that that's what the problem statement says.

20
00:01:26,600 --> 00:01:29,540
But you can also get the size this way you do.

21
00:01:29,840 --> 00:01:33,110
Size of numbers, which is the whole array.

22
00:01:34,240 --> 00:01:37,900
Divided by size of numbers.

23
00:01:38,950 --> 00:01:39,530
Zero.

24
00:01:39,550 --> 00:01:44,410
So we choose any way we can really choose any one of these, but I normally just do number zero.

25
00:01:44,620 --> 00:01:50,980
So what we're doing is we are dividing and you've seen this in the array lecture, I believe, a while

26
00:01:50,980 --> 00:01:51,400
back.

27
00:01:53,140 --> 00:01:59,080
But we take the total size of the array and we divide it by the size of one of the elements in the array.

28
00:02:00,420 --> 00:02:04,200
So that can basically tell you how many things are in it, right?

29
00:02:04,500 --> 00:02:09,900
Because we take the total of all these together, then divide it by the size of one of them.

30
00:02:09,910 --> 00:02:13,350
Each one of these integers will have the same size and memory.

31
00:02:13,830 --> 00:02:17,460
So therefore, we get the number of items in the array.

32
00:02:20,230 --> 00:02:25,750
So this should be compiled, I believe.

33
00:02:28,010 --> 00:02:29,860
Thanks to Z.

34
00:02:30,590 --> 00:02:37,700
So I'm going to go ahead and run this to show you what's going on, and then we will talk about the

35
00:02:37,700 --> 00:02:38,150
function.

36
00:02:40,250 --> 00:02:45,020
So thanks to see so it says into three numbers separated by spaces.

37
00:02:45,350 --> 00:02:48,650
I'm just going to say one to three.

38
00:02:50,910 --> 00:02:57,720
So that is what we expect, right the same as six one plus two is three, three plus three or six.

39
00:02:59,100 --> 00:03:03,420
So what's going on with this function here and how were we able to pass this array?

40
00:03:04,170 --> 00:03:08,760
Well, you notice that right here I print out erase some equals.

41
00:03:09,270 --> 00:03:12,330
I'm also doing something that you might have not seen before.

42
00:03:12,780 --> 00:03:23,420
What I do is, rather than put in some integer here like and answer equals, get some numbers size.

43
00:03:24,600 --> 00:03:30,540
What I do is I just put the function call straight in the see our statement, so you can do that as

44
00:03:30,540 --> 00:03:30,960
well.

45
00:03:31,230 --> 00:03:36,360
You don't have to save it to a variable first and then put answer here in the highlighted section.

46
00:03:36,630 --> 00:03:38,370
You can just put this.

47
00:03:39,450 --> 00:03:43,560
Function call right off the bat without having to make an extra variable.

48
00:03:43,650 --> 00:03:47,910
So I want to point out as well to let's take a look at the function call, though it doesn't really

49
00:03:47,910 --> 00:03:49,000
look that strange, right?

50
00:03:49,020 --> 00:03:51,600
We're just passing two things to it.

51
00:03:52,260 --> 00:03:54,540
We don't tell the function.

52
00:03:54,540 --> 00:03:56,250
Call that it's an array.

53
00:03:58,030 --> 00:04:00,940
All we do is we pass the name of the array.

54
00:04:01,000 --> 00:04:03,820
This is just the variable name for the array, right?

55
00:04:03,820 --> 00:04:04,690
The array is called.

56
00:04:05,230 --> 00:04:07,240
So we pass that as the first argument.

57
00:04:07,840 --> 00:04:10,870
The second argument is our integer size.

58
00:04:11,740 --> 00:04:12,700
Why did I need that?

59
00:04:12,730 --> 00:04:14,980
Well, because I need to use it in the loop here.

60
00:04:14,980 --> 00:04:18,760
I need to know how big the array is, how many items are in the array.

61
00:04:18,790 --> 00:04:20,960
So let's jump up to where we call.

62
00:04:20,990 --> 00:04:25,600
We call, get some list, jump into the implementation of the function, the function definition.

63
00:04:27,060 --> 00:04:28,690
So it returns an integer.

64
00:04:28,710 --> 00:04:30,980
It's called Get Some, of course, we saw that already.

65
00:04:31,350 --> 00:04:33,120
Here's the first parameter.

66
00:04:33,270 --> 00:04:36,990
So the first argument is NUM's that gets sent up here.

67
00:04:37,350 --> 00:04:44,240
The way that we declare an array here in the function parameters is to have some sort of name.

68
00:04:44,250 --> 00:04:47,250
Remember, it doesn't have to do with the name of the variable.

69
00:04:47,520 --> 00:04:49,440
This doesn't have to be called size.

70
00:04:49,620 --> 00:04:51,010
I can just call this s.

71
00:04:51,030 --> 00:04:53,520
Of course, I'd need to change this to s right here.

72
00:04:53,940 --> 00:04:58,440
But you don't need to have it bound to the variable name.

73
00:04:58,440 --> 00:04:59,880
It's about the position, right?

74
00:05:00,660 --> 00:05:07,800
If this is the first argument that this whatever value gets passed as the first argument is going to

75
00:05:07,800 --> 00:05:12,600
match this right here, this is where it's going to be stored when you're doing stuff in the function.

76
00:05:13,080 --> 00:05:14,700
Same with the second position.

77
00:05:14,940 --> 00:05:20,790
Whatever we pass as the second argument is going to be held by the second parameter here.

78
00:05:23,060 --> 00:05:27,900
So this air holds are a Ray Nums.

79
00:05:28,790 --> 00:05:31,300
So what we need to do is put the brackets.

80
00:05:31,310 --> 00:05:36,500
This is one way of passing an array to a function.

81
00:05:36,830 --> 00:05:44,150
It's not the only way, but it's what I'm going to go over first because I feel like this is one of

82
00:05:44,150 --> 00:05:47,120
the more simple ways for you to understand.

83
00:05:47,600 --> 00:05:52,790
Later on, we will be talking about other ways of passing things to the function.

84
00:05:52,790 --> 00:05:58,370
In fact, in this lecture, we're going to talk about a few or at least one other way of passing things

85
00:05:58,370 --> 00:05:59,090
to a function.

86
00:05:59,540 --> 00:06:07,010
But the arrays can be different, and we're going to get into things that have to do with memory management

87
00:06:07,010 --> 00:06:08,840
and stuff like that very soon.

88
00:06:08,840 --> 00:06:13,770
So do not think that this is the only way that you can pass an array to a function.

89
00:06:14,000 --> 00:06:17,030
In fact, you might have already looked up how to do this.

90
00:06:17,030 --> 00:06:23,600
Like I was saying, if you solved the previous problem with an array, you might have already looked

91
00:06:23,600 --> 00:06:24,740
at a different way.

92
00:06:24,770 --> 00:06:28,100
You might have looked online and said, Oh, hey, this isn't what I saw online.

93
00:06:28,100 --> 00:06:29,330
I saw a different way to pass it.

94
00:06:29,720 --> 00:06:32,810
There are other ways, but this is the way that I'm showing you today.

95
00:06:34,130 --> 00:06:36,200
You just put some empty brackets here.

96
00:06:36,470 --> 00:06:42,050
You don't have to define the size, although you could define the size to be size three.

97
00:06:42,380 --> 00:06:43,520
You do not have to.

98
00:06:43,880 --> 00:06:47,510
What we do instead here is we're passing this size.

99
00:06:47,780 --> 00:06:49,790
So we have our size of the array right here.

100
00:06:49,970 --> 00:06:55,590
That way, we are able to do a proper loop where we loop as long as we're less than the size.

101
00:06:55,610 --> 00:06:57,710
As long as I is less than the size, right?

102
00:06:58,880 --> 00:07:02,620
So this is the syntax that I'm showing you to pass an array.

103
00:07:02,630 --> 00:07:05,810
You'll just put some name for the array.

104
00:07:06,410 --> 00:07:11,360
You need to, of course, put the data type of the array, and an array has a specific data type.

105
00:07:11,390 --> 00:07:15,800
This one is an integer array of integers, so therefore you have to put an integer here.

106
00:07:16,490 --> 00:07:24,470
So you say int are empty square brackets or you can put a three here if it matches the size.

107
00:07:25,970 --> 00:07:30,650
I'm also passing this because the for loop, like we said, what do I do inside of here?

108
00:07:30,650 --> 00:07:32,870
I have a sum that starts to zero.

109
00:07:32,870 --> 00:07:37,760
I pretty much do the same thing as the previous problem we looked at, except this time we're adding

110
00:07:37,760 --> 00:07:39,110
straight up integers, right?

111
00:07:39,470 --> 00:07:46,010
We're grabbing each number out of the array and adding it to this accumulator variable that starts at

112
00:07:46,010 --> 00:07:51,170
zero and keeps getting stuff added to it to build up to a sum.

113
00:07:51,470 --> 00:07:58,730
We return that sum and this sum that gets return, it gets passed back straight to here like always

114
00:07:58,730 --> 00:07:59,090
right?

115
00:07:59,420 --> 00:08:01,880
That's what happens if we had this line.

116
00:08:03,190 --> 00:08:10,930
Right here, uncommitted, what it would do is it would pass it right here, and the answer for some

117
00:08:11,050 --> 00:08:14,170
would get saved into this answer variable rate.

118
00:08:14,440 --> 00:08:18,400
However, we're not using this line, so what happens is same thing.

119
00:08:19,300 --> 00:08:27,310
This return value of the sum gets passed straight to here, so you can just imagine all this highlighted

120
00:08:27,310 --> 00:08:30,730
section gets replaced with the sum from the function.

121
00:08:31,720 --> 00:08:37,180
And that is what we print out, it says array space, some space equals space.

122
00:08:37,720 --> 00:08:43,630
And then it prints out the actual integer sum that got returned by this function.

123
00:08:46,000 --> 00:08:49,180
OK, so hopefully that makes sense to you.

124
00:08:49,540 --> 00:08:52,480
Passing arrays not too difficult.

125
00:08:53,440 --> 00:08:58,420
You call the function the same exact way you give it the data the same way.

126
00:08:58,660 --> 00:09:01,810
Remember, it has to do with the position of the arguments.

127
00:09:03,020 --> 00:09:09,740
So the parameters are totally based on positional arguments coming in, so this gets passed to the first

128
00:09:09,740 --> 00:09:10,580
position here.

129
00:09:10,910 --> 00:09:13,190
This gets passed to the second position here.

130
00:09:13,190 --> 00:09:14,840
If you had a third one, it would get passed.

131
00:09:15,380 --> 00:09:20,630
You need another comma here and you'd need kind of the comma here, and you could pass something here

132
00:09:20,630 --> 00:09:23,650
in the third position to the third position over here, right?

133
00:09:24,980 --> 00:09:26,210
So on and so forth.

134
00:09:28,130 --> 00:09:35,180
But the main thing to take away, if you don't already know another way to pass an array is just to

135
00:09:35,180 --> 00:09:41,480
put these empty square brackets here and most likely all the time you're going to want to also pass

136
00:09:41,480 --> 00:09:43,490
the size of the array to the function.

137
00:09:44,590 --> 00:09:49,660
OK, so hopefully this makes sense to you, try to come up with a simple example.

138
00:09:51,730 --> 00:09:52,240
All right.

139
00:09:52,690 --> 00:10:02,580
So let's go ahead and head back to the lecture, so we are going to continue a new topic about functions.

140
00:10:02,590 --> 00:10:07,480
There are two different ways to pass things that I'm going to talk about right now.

141
00:10:07,750 --> 00:10:14,920
There's actually another way or maybe another two ways to pass things, but that's going to be a future

142
00:10:14,920 --> 00:10:16,120
concept that we will teach.

143
00:10:17,230 --> 00:10:20,530
So let's just talk about these two ways right now.

144
00:10:21,250 --> 00:10:27,100
So an important concept with how you have to pass data to a function, how you can pass data to a function

145
00:10:27,370 --> 00:10:34,450
is the fact that you can pass the actual data or you can pass a copy of the data.

146
00:10:35,640 --> 00:10:38,130
Here's the data, the original data.

147
00:10:38,610 --> 00:10:38,920
Right.

148
00:10:38,940 --> 00:10:43,650
That's what I represent with this circle, so let's say we have a function argument, right?

149
00:10:45,270 --> 00:10:53,190
And we also have some matching parameter in the function definition, the function implementation.

150
00:10:53,190 --> 00:10:55,740
That's the part where we define the function.

151
00:10:55,740 --> 00:10:57,000
We have the parameters.

152
00:10:57,010 --> 00:11:00,230
Remember that parameters are where we defined the function.

153
00:11:01,600 --> 00:11:04,300
The arguments are where we call the function, so.

154
00:11:06,060 --> 00:11:08,130
You can have this data.

155
00:11:08,850 --> 00:11:18,030
And if you want to pass the data itself by that, I mean, you're referring to the actual data in memory.

156
00:11:18,580 --> 00:11:18,930
Right?

157
00:11:20,130 --> 00:11:22,860
So you're referring to that piece of memory.

158
00:11:24,470 --> 00:11:28,610
You're going to pass that that thing in that piece of memory to the function.

159
00:11:29,960 --> 00:11:33,560
You can call this is something that you call pass by reference.

160
00:11:35,160 --> 00:11:40,110
And you're going to I'm going to show you the syntax in just a second, but it uses an ampersand, so

161
00:11:40,110 --> 00:11:49,560
that's how we refer to passing the actual memory location of the data we're passing like a reference

162
00:11:49,950 --> 00:11:50,880
to the data.

163
00:11:51,360 --> 00:11:59,250
It's kind of like when you refer to something like but in this case, we're referring to like the actual

164
00:11:59,250 --> 00:12:05,670
thing when we don't include the ampersand, which is what we've been doing so far, we haven't included

165
00:12:05,670 --> 00:12:06,510
the ampersand.

166
00:12:07,170 --> 00:12:12,480
What, interestingly enough, what's been happening is we haven't been actually passing the real data.

167
00:12:14,470 --> 00:12:22,060
So when we passed that size integer to the function, we were not passing the real size variable, we

168
00:12:22,060 --> 00:12:27,700
weren't passing that like same piece of memory, you know, somewhere in the computer's memory, we

169
00:12:27,700 --> 00:12:29,230
were storing that size variable.

170
00:12:29,230 --> 00:12:30,310
We created it, right?

171
00:12:31,060 --> 00:12:35,650
So somewhere in the memory, it made some space and it put that number there, right?

172
00:12:35,650 --> 00:12:37,440
That number was three.

173
00:12:37,450 --> 00:12:42,610
In this case, it was like we used the whole size of thing, but it ended up being three, right?

174
00:12:43,360 --> 00:12:45,880
So the integer three was stored somewhere in memory.

175
00:12:46,750 --> 00:12:52,840
But what we did is rather than passing that thing in memory, that exact memory spot, what we did was

176
00:12:52,840 --> 00:12:56,950
we made a copy of the Value three, so it's in some other piece of memory now.

177
00:12:58,070 --> 00:13:01,970
And we passed that copy, this is called pass by value.

178
00:13:03,130 --> 00:13:07,210
So when you don't use an ampersand, you just we didn't use an ampersand, right, we just put into

179
00:13:07,210 --> 00:13:08,590
size as the parameter.

180
00:13:08,890 --> 00:13:12,520
And then we just put the word size in the arguments.

181
00:13:14,080 --> 00:13:18,850
That's what we did, we passed a copy, it made a copy, C++ make copy, then it passed that copy to

182
00:13:18,850 --> 00:13:19,300
the function.

183
00:13:20,470 --> 00:13:21,960
So let's go ahead and take this.

184
00:13:21,970 --> 00:13:23,410
Take a look at this in code.

185
00:13:23,650 --> 00:13:24,620
So what does this look like?

186
00:13:24,700 --> 00:13:26,200
You go back to the editor again.

187
00:13:26,200 --> 00:13:27,820
We're just bouncing back and forth.

188
00:13:28,390 --> 00:13:30,400
I got this example right here.

189
00:13:32,330 --> 00:13:32,840
So.

190
00:13:34,340 --> 00:13:37,290
What we're doing here is we're making two integers in Maine, right?

191
00:13:37,310 --> 00:13:38,960
We have Int I one equals five.

192
00:13:39,560 --> 00:13:41,330
Then we have two equals seven.

193
00:13:42,700 --> 00:13:47,440
We call this function right here, it's a void function, it does not return anything.

194
00:13:48,570 --> 00:13:55,770
The only thing that it does is it attempts to change the value of the variables that were passed to

195
00:13:55,770 --> 00:13:55,950
it.

196
00:13:57,710 --> 00:13:58,940
Here are the arguments.

197
00:13:59,180 --> 00:14:00,260
I want I to.

198
00:14:01,810 --> 00:14:04,480
Here are the parameters.

199
00:14:04,690 --> 00:14:12,220
They're also called I wanted to, but remember, once again, once again, the names are not significant.

200
00:14:12,880 --> 00:14:16,020
It does not matter that they have the same name that is meaningless.

201
00:14:16,030 --> 00:14:19,180
I just so happened to call them the same exact name.

202
00:14:19,480 --> 00:14:24,610
This could be like I two million and I like, you know, that's probably way too big.

203
00:14:24,610 --> 00:14:29,920
But let's just say I 100 and I 200, you could just call it that same thing doesn't matter.

204
00:14:31,000 --> 00:14:32,800
It's all about the position, right?

205
00:14:32,920 --> 00:14:34,120
This is the first thing.

206
00:14:34,570 --> 00:14:35,640
This is the first thing.

207
00:14:35,650 --> 00:14:36,890
This is the second thing.

208
00:14:36,910 --> 00:14:38,400
This is the second thing they match.

209
00:14:38,410 --> 00:14:40,660
This second thing goes to hear this first thing goes to here.

210
00:14:41,260 --> 00:14:42,310
I want to reiterate that.

211
00:14:42,310 --> 00:14:43,260
So that's very clear.

212
00:14:43,270 --> 00:14:43,990
They understand that.

213
00:14:45,460 --> 00:14:48,880
But what is going on this time, we have the ampersand here.

214
00:14:49,420 --> 00:14:56,800
So now we're passing by reference, we're referring to the actual memory where these variables were

215
00:14:56,800 --> 00:14:59,230
stored when they were created in Maine.

216
00:15:01,230 --> 00:15:03,780
Let's remember back to the scope thing, right?

217
00:15:04,880 --> 00:15:12,440
The scope of what's in between the curly braces I told you that Maine knows about, I want an eye to

218
00:15:12,500 --> 00:15:14,090
these, I want an eye to write.

219
00:15:14,090 --> 00:15:18,110
It knows about these specific versions of I want an eye to.

220
00:15:18,830 --> 00:15:21,470
It made a variable called I want an eye to hear.

221
00:15:22,850 --> 00:15:27,080
A function it does not know about these highlighted variables here.

222
00:15:27,290 --> 00:15:31,160
It only knows about these things here.

223
00:15:33,170 --> 00:15:34,220
But.

224
00:15:35,850 --> 00:15:37,590
What's happening this time?

225
00:15:38,730 --> 00:15:48,000
Is that when we send the arguments I won and I to which are these two things I won, I won, is this

226
00:15:48,000 --> 00:15:48,810
one right here?

227
00:15:49,990 --> 00:15:52,150
And it, too, is this one right here.

228
00:15:53,850 --> 00:15:55,980
Since we included these Ambersons.

229
00:15:57,980 --> 00:16:05,870
These this I want in this, I too are actually the same exact ones in memory as this.

230
00:16:05,870 --> 00:16:06,560
I wanted this to.

231
00:16:06,890 --> 00:16:15,860
They really are this one and this one, even though normally this variable created in this local scope

232
00:16:15,860 --> 00:16:16,550
of main.

233
00:16:18,310 --> 00:16:24,910
Would not be these local variables would not be accessible by another function in this case, since

234
00:16:24,910 --> 00:16:30,520
we passed them by reference, we are referring to the actual variables coming from me.

235
00:16:31,060 --> 00:16:35,080
We're not copying a five into another variable to send up here.

236
00:16:35,350 --> 00:16:39,010
We're not copying the seven four i2 into another variable.

237
00:16:40,030 --> 00:16:42,760
This i1 is really the same as this i1.

238
00:16:42,760 --> 00:16:46,990
This i2 is really the same exact piece of memory as this, same to this same things.

239
00:16:47,830 --> 00:16:48,370
So.

240
00:16:50,930 --> 00:16:57,410
What I want to show you is the interesting thing about this is what happens when we do code like this,

241
00:16:58,190 --> 00:17:02,090
we say I one equals two and I two equals two.

242
00:17:03,830 --> 00:17:11,330
The thing that should have us being pretty curious right now is, are these things going to change because

243
00:17:11,330 --> 00:17:12,620
down here in Maine?

244
00:17:13,890 --> 00:17:20,160
We're kind of got to ask the question, well, is this I one the same as this one, it should be right,

245
00:17:20,160 --> 00:17:22,250
because we're still in Maine right here.

246
00:17:22,260 --> 00:17:29,040
So when I print out, I won, you know, ideally it would have this value.

247
00:17:29,040 --> 00:17:30,570
Five That's what makes sense, right?

248
00:17:31,560 --> 00:17:33,450
Although I sent it to this function.

249
00:17:35,390 --> 00:17:42,140
So if I send it to this function by reference, the question is when I say I one equals two, does this

250
00:17:42,140 --> 00:17:46,340
I one down in Maine now equal two instead of five?

251
00:17:47,390 --> 00:17:54,650
Same with the I two does I two down here in Maine when I printed out, will it stay say seven?

252
00:17:55,070 --> 00:17:56,360
Or will it say two?

253
00:17:56,930 --> 00:17:59,750
This all depends on whether we put this ampersand here.

254
00:18:00,590 --> 00:18:01,940
So let's test it out, right?

255
00:18:01,970 --> 00:18:05,630
I'm going to go ahead and clear this, and I'm going to go ahead and compile.

256
00:18:07,010 --> 00:18:08,930
This is called reference value CBP.

257
00:18:08,930 --> 00:18:12,330
And I was going to say RV e.

258
00:18:15,580 --> 00:18:16,120
Oh, yeah.

259
00:18:16,150 --> 00:18:17,230
I made a mistake.

260
00:18:17,470 --> 00:18:18,490
I need to put these.

261
00:18:19,040 --> 00:18:20,210
Making a lot of mistakes here.

262
00:18:20,230 --> 00:18:20,920
Sorry about that.

263
00:18:21,280 --> 00:18:22,270
So I'm going to say this.

264
00:18:23,980 --> 00:18:25,000
Go ahead and compiled again.

265
00:18:26,140 --> 00:18:26,760
We're all going to go.

266
00:18:26,770 --> 00:18:30,850
I'm going to clear and then I'm going to run this RV.

267
00:18:31,840 --> 00:18:36,100
And before I run it, I want you to try and guess what's going to happen.

268
00:18:38,180 --> 00:18:38,780
So here we go.

269
00:18:40,720 --> 00:18:41,560
We'll look at that.

270
00:18:42,130 --> 00:18:44,890
I won, and I too are actually too.

271
00:18:46,120 --> 00:18:49,840
Some of you may be thinking, well, of course, that makes sense.

272
00:18:51,160 --> 00:18:55,600
You passed them to the function and then you changed him in the function, why would you expect them

273
00:18:55,600 --> 00:18:56,530
to be the same thing?

274
00:18:56,540 --> 00:18:57,430
They got changed.

275
00:18:57,850 --> 00:19:04,270
Well, let's see what happens when I remove the ampersand for the eye to.

276
00:19:06,060 --> 00:19:09,460
I'm still passing the same arguments haven't changed that, right?

277
00:19:09,480 --> 00:19:16,200
I'm still saying pass, I one up here to this position, pass it to appear at this position.

278
00:19:16,200 --> 00:19:21,420
The only thing that I've changed is I got rid of the ampersand in front of the variable name and this

279
00:19:21,420 --> 00:19:25,710
parameter for it to go ahead and save this.

280
00:19:27,160 --> 00:19:31,270
Let's go ahead and compile it again and run it again.

281
00:19:32,820 --> 00:19:33,750
We'll look at that.

282
00:19:34,730 --> 00:19:42,240
Looks like I won and did get changed it, too, but I too did not get changed to two.

283
00:19:42,260 --> 00:19:43,390
It's still seven.

284
00:19:43,400 --> 00:19:49,700
It's still stayed, stayed the same value as the local scope variable rate.

285
00:19:49,910 --> 00:19:52,730
So this was this variable is a local variable to Maine.

286
00:19:53,760 --> 00:19:58,890
Since we did not pass it by reference, it did not actually change the value.

287
00:19:59,430 --> 00:20:03,330
This was passed by value, it was a copy this time.

288
00:20:04,230 --> 00:20:05,010
I won.

289
00:20:06,180 --> 00:20:09,540
It passed the real eye one in memory, right?

290
00:20:09,570 --> 00:20:14,980
It passed that real, real variable there because of the on this issue.

291
00:20:15,210 --> 00:20:16,560
It passed a copy.

292
00:20:17,570 --> 00:20:19,850
Of it, it did not, it's in a different spot.

293
00:20:20,120 --> 00:20:25,610
It's technically like another variable as the same value, but it's it's not the same exact variable.

294
00:20:26,660 --> 00:20:30,500
So what happened was we just changed the copy of a two to two.

295
00:20:31,370 --> 00:20:38,780
So when we go back down here, C++ prints out this I too, because it didn't really, you know, it

296
00:20:38,780 --> 00:20:40,820
prints out this issue and it didn't get changed.

297
00:20:40,820 --> 00:20:43,250
It's still seven because we didn't really pass the real thing.

298
00:20:43,250 --> 00:20:45,110
We just passed a copy and it changed the copy.

299
00:20:45,110 --> 00:20:46,910
And that didn't affect this one right here.

300
00:20:46,910 --> 00:20:47,360
It did not.

301
00:20:47,360 --> 00:20:48,470
This did not get affected.

302
00:20:49,490 --> 00:20:52,850
However, this one did because we passed the actual eye.

303
00:20:54,270 --> 00:20:54,610
Right.

304
00:20:54,630 --> 00:21:00,800
And it's not really a it's not really that we passed the actual eye one we just put I won in the arguments

305
00:21:01,020 --> 00:21:01,770
right here, right?

306
00:21:02,130 --> 00:21:06,630
Except the fact that the parameter has an ampersand up here in the definition.

307
00:21:08,040 --> 00:21:15,000
It's so happens that since that's in the parameters of the definition that it it saw it as the actual

308
00:21:15,000 --> 00:21:17,100
value should be used, i one.

309
00:21:18,350 --> 00:21:24,800
OK, so the ampersand goes up here in the parameters, all right, for what we're talking about now

310
00:21:24,800 --> 00:21:27,170
for the purposes of this pass by reference.

311
00:21:27,860 --> 00:21:32,540
You're going to put the ampersand up here, OK, in the parameters if you want to pass by reference.

312
00:21:32,540 --> 00:21:36,710
So if you want your variable to change.

313
00:21:38,310 --> 00:21:44,490
In another function, you're going to need to pass it by reference what you would have to do if you

314
00:21:44,490 --> 00:21:48,360
passed it by value is if you'd have to maybe have this not be void.

315
00:21:49,390 --> 00:21:51,580
You have to have it like integer, right?

316
00:21:52,240 --> 00:21:57,400
And what you would do is you could say like return I to.

317
00:21:59,350 --> 00:22:01,720
And then what you could say is.

318
00:22:03,520 --> 00:22:12,880
Let's say that you do like and new IQ equals.

319
00:22:15,650 --> 00:22:16,490
A function

320
00:22:19,100 --> 00:22:21,620
I won I to.

321
00:22:22,870 --> 00:22:23,260
All right.

322
00:22:23,470 --> 00:22:24,910
And then, ah, my comment this out.

323
00:22:26,810 --> 00:22:28,190
So let's see what happens now.

324
00:22:30,380 --> 00:22:33,970
Save or actually know what I want to do now is I want to say I knew it too.

325
00:22:35,060 --> 00:22:36,080
I'm actually going to do this.

326
00:22:36,080 --> 00:22:38,050
I'm going to say new I to right here, right?

327
00:22:39,560 --> 00:22:41,030
So we have I too.

328
00:22:41,060 --> 00:22:41,750
It was the same.

329
00:22:41,750 --> 00:22:44,240
And then we have this new idea that got saved right here, right?

330
00:22:45,020 --> 00:22:46,700
So now I'm going to go ahead and save this.

331
00:22:48,020 --> 00:22:54,530
So now the function returns an integer, what it does is it returns the AI to coming from here.

332
00:22:55,280 --> 00:22:57,200
So this got passed as a copy.

333
00:22:57,500 --> 00:22:59,060
This got passed as a reference.

334
00:23:00,570 --> 00:23:02,250
So let's let's.

335
00:23:03,300 --> 00:23:06,870
How do you think about what what's going to actually print out this time?

336
00:23:06,990 --> 00:23:13,050
So try and take a guess it was going to show up here in the console, going to compile this.

337
00:23:13,720 --> 00:23:14,670
I'll go ahead and run it.

338
00:23:14,670 --> 00:23:17,760
So I mean, positive video if you need to think more about it.

339
00:23:20,170 --> 00:23:24,940
And yet that ad didn't have a space, so it was kind of gross, but you kind of saw what happened,

340
00:23:24,940 --> 00:23:25,180
right?

341
00:23:25,780 --> 00:23:27,490
Let's go ahead and say this again.

342
00:23:27,910 --> 00:23:39,340
And let's compile and once again made the same mistake, really not doing that great with these mistakes.

343
00:23:39,640 --> 00:23:41,450
OK, so there we go.

344
00:23:41,470 --> 00:23:43,180
Two and then a seven and then two.

345
00:23:43,420 --> 00:23:48,220
So what happened is that we called a function.

346
00:23:48,670 --> 00:23:53,320
We gave it the eye one and I to their local to main.

347
00:23:54,160 --> 00:24:00,820
So we sent I one up here and we sent it to up to this position when it went up to this function.

348
00:24:01,240 --> 00:24:05,620
C++ recognized one as being the actual variable in memory.

349
00:24:06,500 --> 00:24:07,670
Coming from Maine.

350
00:24:08,870 --> 00:24:15,770
It's so I, too, as just a value, right, it just saw a two as seven, it's like, oh, the number

351
00:24:15,770 --> 00:24:16,610
seven is here.

352
00:24:17,120 --> 00:24:19,700
It went down here and it said, I one equals two.

353
00:24:19,730 --> 00:24:27,680
All right, I'm going to change the actual variable in memory I wanted to do and says I two equals two.

354
00:24:29,270 --> 00:24:32,410
All I'm going to do is I'm going to take this new variable here.

355
00:24:32,420 --> 00:24:35,930
This is like a new variable that's now local to this function.

356
00:24:36,920 --> 00:24:42,080
It's not it doesn't have anything to do with Maine, it's the local to this function called a function.

357
00:24:43,230 --> 00:24:45,630
So it's only in the scope of this function.

358
00:24:47,210 --> 00:24:52,200
So what it does is it says this new variable, it's here, it's a copy, I'm going to change that to

359
00:24:52,200 --> 00:24:53,810
two, right?

360
00:24:54,760 --> 00:24:59,020
And then it says, Oh, I'm actually going to return that now I'm going to return this copy that I just

361
00:24:59,020 --> 00:25:02,320
changed it to, and so that answer comes down to here.

362
00:25:02,320 --> 00:25:07,210
This return value I two equals two two is stored in here.

363
00:25:07,810 --> 00:25:11,050
That value of two goes back down to here.

364
00:25:12,570 --> 00:25:15,870
And it gets saved into this new eye to.

365
00:25:17,410 --> 00:25:19,900
We then go down to print stuff out.

366
00:25:20,770 --> 00:25:22,210
It prints out, I won.

367
00:25:22,270 --> 00:25:25,390
The fact is is that I won actually got changed.

368
00:25:25,390 --> 00:25:30,610
This I won, got changed to two up here because we passed it by reference.

369
00:25:30,610 --> 00:25:32,320
So it prints out to.

370
00:25:33,850 --> 00:25:36,550
I too, is this I too right here.

371
00:25:37,030 --> 00:25:42,910
It's still seven because we just passed some copy and saved it into a new variable right here in this

372
00:25:42,910 --> 00:25:43,450
parameter.

373
00:25:44,870 --> 00:25:50,360
So this just prints out seven, because this this variable that's local to Maine.

374
00:25:51,760 --> 00:25:54,070
Didn't really get changed, it was not a reference.

375
00:25:56,220 --> 00:25:57,280
We put another comma.

376
00:25:57,300 --> 00:25:58,980
Then we print out new AI, too.

377
00:25:59,100 --> 00:26:06,870
Well, new AI to actually has this value because what happened is that this copy got changed right here.

378
00:26:07,350 --> 00:26:09,630
This copy is local to this function.

379
00:26:09,930 --> 00:26:10,740
It got changed.

380
00:26:10,740 --> 00:26:17,460
But then it what it did is it passed that answer that value down to here and then it got saved in here.

381
00:26:18,390 --> 00:26:24,870
Now, this is a new variable that's within the scope of Maine that has the answer that got saved into

382
00:26:24,870 --> 00:26:25,020
it.

383
00:26:25,590 --> 00:26:26,850
So when we print that out?

384
00:26:27,950 --> 00:26:29,780
It stores the answer to right.

385
00:26:29,930 --> 00:26:31,190
So that's why you see the two there.

386
00:26:32,980 --> 00:26:38,890
So this might be pretty confusing, but if you're going to take just one thing away from it, remember

387
00:26:38,890 --> 00:26:44,260
that when you pass something by reference, it enables you to change the actual variable.

388
00:26:45,560 --> 00:26:52,940
That was coming from the function that called this other function right?

389
00:26:53,630 --> 00:26:55,520
So we called a function.

390
00:26:55,550 --> 00:27:01,460
It's actually passing this re actual variable is passing the memory OK?

391
00:27:01,790 --> 00:27:03,290
It's like the memory location.

392
00:27:03,890 --> 00:27:07,410
We didn't talk a lot more about memory locations, right?

393
00:27:07,430 --> 00:27:14,360
And the difference between having just a value and actually referring to a place and memory, a piece

394
00:27:14,360 --> 00:27:15,770
like a chunk of memory, right?

395
00:27:16,100 --> 00:27:17,510
We've got those bytes.

396
00:27:18,560 --> 00:27:20,510
Of space in memory, right?

397
00:27:21,230 --> 00:27:26,900
We're going to have we've barely gone over the computer memory stuff, but we're about to go in deep

398
00:27:27,470 --> 00:27:32,420
into that and we're going to talk about the different sections of memory inside the computer.

399
00:27:32,630 --> 00:27:35,750
We're talking about virtual address spaces and in depth.

400
00:27:37,000 --> 00:27:42,410
You know what's really happening when you load your program into memory and run it, you know?

401
00:27:43,780 --> 00:27:48,130
So that's all I have to say about passed by reference and passed by value.

402
00:27:50,710 --> 00:27:54,220
So let's go ahead and head back.

403
00:27:56,000 --> 00:27:59,960
And I also wanted to let's see.

404
00:28:01,730 --> 00:28:06,800
So this is just a slide of us heading over to check it out, which is what we did.

405
00:28:07,250 --> 00:28:09,230
But this problem?

406
00:28:09,530 --> 00:28:11,840
Let's see this problem that you solved.

407
00:28:11,990 --> 00:28:17,420
Something that I kind of forgot is if you wanted to take a look at the table and kind of add up the

408
00:28:17,420 --> 00:28:19,400
values for that.

409
00:28:20,590 --> 00:28:22,690
You can whip out a calculator here.

410
00:28:23,690 --> 00:28:28,010
And you can add these values together.

411
00:28:29,230 --> 00:28:33,830
These are like this specific one was like a zebra.

412
00:28:33,850 --> 00:28:36,050
I think it's one of the words I entered.

413
00:28:36,070 --> 00:28:39,820
So let's let's go ahead and do that, actually, so I'm going to head back here.

414
00:28:39,970 --> 00:28:46,540
It's kind of out of order, but I would like to kind of go back to this too and cover that.

415
00:28:46,540 --> 00:28:46,870
So.

416
00:28:50,640 --> 00:28:58,590
What I want to do is show you an example of this, so I think that we.

417
00:29:00,610 --> 00:29:01,330
Had.

418
00:29:09,240 --> 00:29:14,010
I'm just going to compile this just in case so

419
00:29:17,240 --> 00:29:20,090
funky are so I'm going to call this funk.

420
00:29:21,230 --> 00:29:22,820
So let's go ahead and run this.

421
00:29:24,200 --> 00:29:30,290
So I'm jumping back kind of covered the last thing that I want to cover in the lecture, but I want

422
00:29:30,290 --> 00:29:35,510
to go over this just so we can kind of double check and you can double check your work for this problem

423
00:29:35,510 --> 00:29:39,860
because this was kind of like a big step forward with you having to solve this problem.

424
00:29:41,150 --> 00:29:43,340
So I'm going to go ahead and enter some words here.

425
00:29:44,060 --> 00:29:51,640
I'm going to enter car, I'm going to enter that and I'm going to enter Dad.

426
00:29:53,540 --> 00:29:58,700
OK, so it said the string with the largest sum is back with a sum of three hundred and eleven.

427
00:29:59,360 --> 00:30:03,560
So what I want to do now is head over to this ASCII table.

428
00:30:03,560 --> 00:30:05,930
This is just on a Wikipedia page.

429
00:30:06,500 --> 00:30:08,510
You can find another ASCII table if you like.

430
00:30:09,570 --> 00:30:12,270
And I'm going to just about this handy dandy calculator.

431
00:30:13,870 --> 00:30:15,220
And I want to show you.

432
00:30:17,380 --> 00:30:21,430
Let's see if we can actually leave all this clear.

433
00:30:24,180 --> 00:30:28,960
So I'm going to clear this stuff right here, so we have these other things that just kind of got saved

434
00:30:28,960 --> 00:30:29,370
off here.

435
00:30:31,550 --> 00:30:35,810
So we got this history, I don't really need the history, but let's go ahead and just let's go ahead

436
00:30:35,810 --> 00:30:36,830
and just calculate our stuff.

437
00:30:36,830 --> 00:30:38,300
So it said that.

438
00:30:39,350 --> 00:30:46,220
That was the string with the largest sum, and that's somewhat 311, so now let's just prove that that

439
00:30:46,220 --> 00:30:47,060
is actually true.

440
00:30:48,020 --> 00:30:51,290
So right here, we have our lowercase characters, right?

441
00:30:51,350 --> 00:30:53,090
So this is what I was talking about here.

442
00:30:53,090 --> 00:30:56,000
This decimal values what we're interested in for the sum.

443
00:30:57,730 --> 00:31:02,290
So I know I'm kind of jumping back here and probably that, but I want to go back over this, so.

444
00:31:03,750 --> 00:31:14,850
We got bee right bees in 98, so we do 98, plus we got a 97, so that's 97 plus.

445
00:31:16,190 --> 00:31:24,730
T is 116, 116 equals three, 11, right, and that's what it said, it said the sum was three, 11.

446
00:31:25,420 --> 00:31:27,370
We have car and dad as well.

447
00:31:29,290 --> 00:31:37,750
So let me go ahead and clear this car is 99 plus 97 for a plus.

448
00:31:39,030 --> 00:31:39,990
114.

449
00:31:43,260 --> 00:31:44,190
That's three, 10.

450
00:31:45,930 --> 00:31:49,080
So that wasn't necessarily one last, that's pretty funny, right?

451
00:31:50,580 --> 00:31:59,160
Then we have dad, right, so dad is just going to be one hundred plus ninety seven plus one hundred

452
00:31:59,160 --> 00:32:00,570
thirty be two ninety seven.

453
00:32:00,810 --> 00:32:01,110
Right.

454
00:32:03,240 --> 00:32:09,250
So just wanted to show you that you can refer to this ASCII table if you are confused on that part.

455
00:32:09,270 --> 00:32:14,750
You can go and look at these decimal values and you can kind of double check manually with a calculator.

456
00:32:14,760 --> 00:32:16,980
What's going on with your program?

457
00:32:19,090 --> 00:32:24,800
So with that, that is kind of the last thing that I wanted to go back and go over.

458
00:32:24,820 --> 00:32:27,940
Hopefully you understand the references thing and.

459
00:32:29,540 --> 00:32:31,340
Understood how to pass an array.

460
00:32:32,850 --> 00:32:39,690
We're going to have another function's lecture, I think, where I just want to go over calling multiple

461
00:32:39,690 --> 00:32:46,230
functions and also the fact that you can call a function within another function just like we've been

462
00:32:46,230 --> 00:32:47,100
doing in main.

463
00:32:48,200 --> 00:32:58,040
But imagine that we call Maine and then we're in Maine, we call, get some and then get some insight

464
00:32:58,130 --> 00:33:00,590
inside here and say, I guess we call another function.

465
00:33:01,370 --> 00:33:02,240
And that's possible.

466
00:33:03,050 --> 00:33:04,010
We can definitely do that.

467
00:33:04,310 --> 00:33:06,650
I'm also going to cover something else called prototyping.

468
00:33:08,000 --> 00:33:12,230
So with that, I will see you in the next lectures.
