An interesting thing is that we can pass a raise to functions as well.
So if you did this problem, you might have some of you.
I'm not expecting it, but some of you might have thought, Hmm, I want to read this into an array,
then I want to send the array of words to the function.
You might have done that.
You might not, but I'm guessing that you had to probably look something up for that, right?
Unless you already knew how to do that in C++, you probably had to go online and see how you can pass
an array to a function or even if you can, which you can.
So let's head to the editor to see what that looks like.
For those of you that haven't done that and it didn't look that up.
So we're going to go over here.
So I have a different program here.
This program actually just calculates the sum of all the integers in an array, so it gets it from the
user, it gets three integers.
So right here we have an array of integers size three called nums.
We tell the user, please enter three numbers separated by spaces, all in one line.
We see into each one of the positions of the array.
Since the array is size three, it has indices zero, one and two.
You could just hard code the size to three, since you know that that's what the problem statement says.
But you can also get the size this way you do.
Size of numbers, which is the whole array.
Divided by size of numbers.
Zero.
So we choose any way we can really choose any one of these, but I normally just do number zero.
So what we're doing is we are dividing and you've seen this in the array lecture, I believe, a while
back.
But we take the total size of the array and we divide it by the size of one of the elements in the array.
So that can basically tell you how many things are in it, right?
Because we take the total of all these together, then divide it by the size of one of them.
Each one of these integers will have the same size and memory.
So therefore, we get the number of items in the array.
So this should be compiled, I believe.
Thanks to Z.
So I'm going to go ahead and run this to show you what's going on, and then we will talk about the
function.
So thanks to see so it says into three numbers separated by spaces.
I'm just going to say one to three.
So that is what we expect, right the same as six one plus two is three, three plus three or six.
So what's going on with this function here and how were we able to pass this array?
Well, you notice that right here I print out erase some equals.
I'm also doing something that you might have not seen before.
What I do is, rather than put in some integer here like and answer equals, get some numbers size.
What I do is I just put the function call straight in the see our statement, so you can do that as
well.
You don't have to save it to a variable first and then put answer here in the highlighted section.
You can just put this.
Function call right off the bat without having to make an extra variable.
So I want to point out as well to let's take a look at the function call, though it doesn't really
look that strange, right?
We're just passing two things to it.
We don't tell the function.
Call that it's an array.
All we do is we pass the name of the array.
This is just the variable name for the array, right?
The array is called.
So we pass that as the first argument.
The second argument is our integer size.
Why did I need that?
Well, because I need to use it in the loop here.
I need to know how big the array is, how many items are in the array.
So let's jump up to where we call.
We call, get some list, jump into the implementation of the function, the function definition.
So it returns an integer.
It's called Get Some, of course, we saw that already.
Here's the first parameter.
So the first argument is NUM's that gets sent up here.
The way that we declare an array here in the function parameters is to have some sort of name.
Remember, it doesn't have to do with the name of the variable.
This doesn't have to be called size.
I can just call this s.
Of course, I'd need to change this to s right here.
But you don't need to have it bound to the variable name.
It's about the position, right?
If this is the first argument that this whatever value gets passed as the first argument is going to
match this right here, this is where it's going to be stored when you're doing stuff in the function.
Same with the second position.
Whatever we pass as the second argument is going to be held by the second parameter here.
So this air holds are a Ray Nums.
So what we need to do is put the brackets.
This is one way of passing an array to a function.
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
the more simple ways for you to understand.
Later on, we will be talking about other ways of passing things to the function.
In fact, in this lecture, we're going to talk about a few or at least one other way of passing things
to a function.
But the arrays can be different, and we're going to get into things that have to do with memory management
and stuff like that very soon.
So do not think that this is the only way that you can pass an array to a function.
In fact, you might have already looked up how to do this.
Like I was saying, if you solved the previous problem with an array, you might have already looked
at a different way.
You might have looked online and said, Oh, hey, this isn't what I saw online.
I saw a different way to pass it.
There are other ways, but this is the way that I'm showing you today.
You just put some empty brackets here.
You don't have to define the size, although you could define the size to be size three.
You do not have to.
What we do instead here is we're passing this size.
So we have our size of the array right here.
That way, we are able to do a proper loop where we loop as long as we're less than the size.
As long as I is less than the size, right?
So this is the syntax that I'm showing you to pass an array.
You'll just put some name for the array.
You need to, of course, put the data type of the array, and an array has a specific data type.
This one is an integer array of integers, so therefore you have to put an integer here.
So you say int are empty square brackets or you can put a three here if it matches the size.
I'm also passing this because the for loop, like we said, what do I do inside of here?
I have a sum that starts to zero.
I pretty much do the same thing as the previous problem we looked at, except this time we're adding
straight up integers, right?
We're grabbing each number out of the array and adding it to this accumulator variable that starts at
zero and keeps getting stuff added to it to build up to a sum.
We return that sum and this sum that gets return, it gets passed back straight to here like always
right?
That's what happens if we had this line.
Right here, uncommitted, what it would do is it would pass it right here, and the answer for some
would get saved into this answer variable rate.
However, we're not using this line, so what happens is same thing.
This return value of the sum gets passed straight to here, so you can just imagine all this highlighted
section gets replaced with the sum from the function.
And that is what we print out, it says array space, some space equals space.
And then it prints out the actual integer sum that got returned by this function.
OK, so hopefully that makes sense to you.
Passing arrays not too difficult.
You call the function the same exact way you give it the data the same way.
Remember, it has to do with the position of the arguments.
So the parameters are totally based on positional arguments coming in, so this gets passed to the first
position here.
This gets passed to the second position here.
If you had a third one, it would get passed.
You need another comma here and you'd need kind of the comma here, and you could pass something here
in the third position to the third position over here, right?
So on and so forth.
But the main thing to take away, if you don't already know another way to pass an array is just to
put these empty square brackets here and most likely all the time you're going to want to also pass
the size of the array to the function.
OK, so hopefully this makes sense to you, try to come up with a simple example.
All right.
So let's go ahead and head back to the lecture, so we are going to continue a new topic about functions.
There are two different ways to pass things that I'm going to talk about right now.
There's actually another way or maybe another two ways to pass things, but that's going to be a future
concept that we will teach.
So let's just talk about these two ways right now.
So an important concept with how you have to pass data to a function, how you can pass data to a function
is the fact that you can pass the actual data or you can pass a copy of the data.
Here's the data, the original data.
Right.
That's what I represent with this circle, so let's say we have a function argument, right?
And we also have some matching parameter in the function definition, the function implementation.
That's the part where we define the function.
We have the parameters.
Remember that parameters are where we defined the function.
The arguments are where we call the function, so.
You can have this data.
And if you want to pass the data itself by that, I mean, you're referring to the actual data in memory.
Right?
So you're referring to that piece of memory.
You're going to pass that that thing in that piece of memory to the function.
You can call this is something that you call pass by reference.
And you're going to I'm going to show you the syntax in just a second, but it uses an ampersand, so
that's how we refer to passing the actual memory location of the data we're passing like a reference
to the data.
It's kind of like when you refer to something like but in this case, we're referring to like the actual
thing when we don't include the ampersand, which is what we've been doing so far, we haven't included
the ampersand.
What, interestingly enough, what's been happening is we haven't been actually passing the real data.
So when we passed that size integer to the function, we were not passing the real size variable, we
weren't passing that like same piece of memory, you know, somewhere in the computer's memory, we
were storing that size variable.
We created it, right?
So somewhere in the memory, it made some space and it put that number there, right?
That number was three.
In this case, it was like we used the whole size of thing, but it ended up being three, right?
So the integer three was stored somewhere in memory.
But what we did is rather than passing that thing in memory, that exact memory spot, what we did was
we made a copy of the Value three, so it's in some other piece of memory now.
And we passed that copy, this is called pass by value.
So when you don't use an ampersand, you just we didn't use an ampersand, right, we just put into
size as the parameter.
And then we just put the word size in the arguments.
That's what we did, we passed a copy, it made a copy, C++ make copy, then it passed that copy to
the function.
So let's go ahead and take this.
Take a look at this in code.
So what does this look like?
You go back to the editor again.
We're just bouncing back and forth.
I got this example right here.
So.
What we're doing here is we're making two integers in Maine, right?
We have Int I one equals five.
Then we have two equals seven.
We call this function right here, it's a void function, it does not return anything.
The only thing that it does is it attempts to change the value of the variables that were passed to
it.
Here are the arguments.
I want I to.
Here are the parameters.
They're also called I wanted to, but remember, once again, once again, the names are not significant.
It does not matter that they have the same name that is meaningless.
I just so happened to call them the same exact name.
This could be like I two million and I like, you know, that's probably way too big.
But let's just say I 100 and I 200, you could just call it that same thing doesn't matter.
It's all about the position, right?
This is the first thing.
This is the second thing.
This is the second thing they match.
This second thing goes to hear this first thing goes to here.
I want to reiterate that.
So that's very clear.
They understand that.
But what is going on this time, we have the ampersand here.
So now we're passing by reference, we're referring to the actual memory where these variables were
stored when they were created in Maine.
Let's remember back to the scope thing, right?
The scope of what's in between the curly braces I told you that Maine knows about, I want an eye to
these, I want an eye to write.
It knows about these specific versions of I want an eye to.
It made a variable called I want an eye to hear.
A function it does not know about these highlighted variables here.
It only knows about these things here.
But.
What's happening this time?
Is that when we send the arguments I won and I to which are these two things I won, I won, is this
one right here?
And it, too, is this one right here.
Since we included these Ambersons.
These this I want in this, I too are actually the same exact ones in memory as this.
I wanted this to.
They really are this one and this one, even though normally this variable created in this local scope
of main.
Would not be these local variables would not be accessible by another function in this case, since
we passed them by reference, we are referring to the actual variables coming from me.
We're not copying a five into another variable to send up here.
We're not copying the seven four i2 into another variable.
This i1 is really the same as this i1.
This i2 is really the same exact piece of memory as this, same to this same things.
So.
What I want to show you is the interesting thing about this is what happens when we do code like this,
we say I one equals two and I two equals two.
The thing that should have us being pretty curious right now is, are these things going to change because
down here in Maine?
We're kind of got to ask the question, well, is this I one the same as this one, it should be right,
because we're still in Maine right here.
So when I print out, I won, you know, ideally it would have this value.
Five That's what makes sense, right?
Although I sent it to this function.
So if I send it to this function by reference, the question is when I say I one equals two, does this
I one down in Maine now equal two instead of five?
Same with the I two does I two down here in Maine when I printed out, will it stay say seven?
Or will it say two?
This all depends on whether we put this ampersand here.
So let's test it out, right?
I'm going to go ahead and clear this, and I'm going to go ahead and compile.
This is called reference value CBP.
And I was going to say RV e.
Oh, yeah.
I made a mistake.
I need to put these.
Making a lot of mistakes here.
Sorry about that.
So I'm going to say this.
Go ahead and compiled again.
We're all going to go.
I'm going to clear and then I'm going to run this RV.
And before I run it, I want you to try and guess what's going to happen.
So here we go.
We'll look at that.
I won, and I too are actually too.
Some of you may be thinking, well, of course, that makes sense.
You passed them to the function and then you changed him in the function, why would you expect them
to be the same thing?
They got changed.
Well, let's see what happens when I remove the ampersand for the eye to.
I'm still passing the same arguments haven't changed that, right?
I'm still saying pass, I one up here to this position, pass it to appear at this position.
The only thing that I've changed is I got rid of the ampersand in front of the variable name and this
parameter for it to go ahead and save this.
Let's go ahead and compile it again and run it again.
We'll look at that.
Looks like I won and did get changed it, too, but I too did not get changed to two.
It's still seven.
It's still stayed, stayed the same value as the local scope variable rate.
So this was this variable is a local variable to Maine.
Since we did not pass it by reference, it did not actually change the value.
This was passed by value, it was a copy this time.
I won.
It passed the real eye one in memory, right?
It passed that real, real variable there because of the on this issue.
It passed a copy.
Of it, it did not, it's in a different spot.
It's technically like another variable as the same value, but it's it's not the same exact variable.
So what happened was we just changed the copy of a two to two.
So when we go back down here, C++ prints out this I too, because it didn't really, you know, it
prints out this issue and it didn't get changed.
It's still seven because we didn't really pass the real thing.
We just passed a copy and it changed the copy.
And that didn't affect this one right here.
It did not.
This did not get affected.
However, this one did because we passed the actual eye.
Right.
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
right here, right?
Except the fact that the parameter has an ampersand up here in the definition.
It's so happens that since that's in the parameters of the definition that it it saw it as the actual
value should be used, i one.
OK, so the ampersand goes up here in the parameters, all right, for what we're talking about now
for the purposes of this pass by reference.
You're going to put the ampersand up here, OK, in the parameters if you want to pass by reference.
So if you want your variable to change.
In another function, you're going to need to pass it by reference what you would have to do if you
passed it by value is if you'd have to maybe have this not be void.
You have to have it like integer, right?
And what you would do is you could say like return I to.
And then what you could say is.
Let's say that you do like and new IQ equals.
A function
I won I to.
All right.
And then, ah, my comment this out.
So let's see what happens now.
Save or actually know what I want to do now is I want to say I knew it too.
I'm actually going to do this.
I'm going to say new I to right here, right?
So we have I too.
It was the same.
And then we have this new idea that got saved right here, right?
So now I'm going to go ahead and save this.
So now the function returns an integer, what it does is it returns the AI to coming from here.
So this got passed as a copy.
This got passed as a reference.
So let's let's.
How do you think about what what's going to actually print out this time?
So try and take a guess it was going to show up here in the console, going to compile this.
I'll go ahead and run it.
So I mean, positive video if you need to think more about it.
And yet that ad didn't have a space, so it was kind of gross, but you kind of saw what happened,
right?
Let's go ahead and say this again.
And let's compile and once again made the same mistake, really not doing that great with these mistakes.
OK, so there we go.
Two and then a seven and then two.
So what happened is that we called a function.
We gave it the eye one and I to their local to main.
So we sent I one up here and we sent it to up to this position when it went up to this function.
C++ recognized one as being the actual variable in memory.
Coming from Maine.
It's so I, too, as just a value, right, it just saw a two as seven, it's like, oh, the number
seven is here.
It went down here and it said, I one equals two.
All right, I'm going to change the actual variable in memory I wanted to do and says I two equals two.
All I'm going to do is I'm going to take this new variable here.
This is like a new variable that's now local to this function.
It's not it doesn't have anything to do with Maine, it's the local to this function called a function.
So it's only in the scope of this function.
So what it does is it says this new variable, it's here, it's a copy, I'm going to change that to
two, right?
And then it says, Oh, I'm actually going to return that now I'm going to return this copy that I just
changed it to, and so that answer comes down to here.
This return value I two equals two two is stored in here.
That value of two goes back down to here.
And it gets saved into this new eye to.
We then go down to print stuff out.
It prints out, I won.
The fact is is that I won actually got changed.
This I won, got changed to two up here because we passed it by reference.
So it prints out to.
I too, is this I too right here.
It's still seven because we just passed some copy and saved it into a new variable right here in this
parameter.
So this just prints out seven, because this this variable that's local to Maine.
Didn't really get changed, it was not a reference.
We put another comma.
Then we print out new AI, too.
Well, new AI to actually has this value because what happened is that this copy got changed right here.
This copy is local to this function.
It got changed.
But then it what it did is it passed that answer that value down to here and then it got saved in here.
Now, this is a new variable that's within the scope of Maine that has the answer that got saved into
it.
So when we print that out?
It stores the answer to right.
So that's why you see the two there.
So this might be pretty confusing, but if you're going to take just one thing away from it, remember
that when you pass something by reference, it enables you to change the actual variable.
That was coming from the function that called this other function right?
So we called a function.
It's actually passing this re actual variable is passing the memory OK?
It's like the memory location.
We didn't talk a lot more about memory locations, right?
And the difference between having just a value and actually referring to a place and memory, a piece
like a chunk of memory, right?
We've got those bytes.
Of space in memory, right?
We're going to have we've barely gone over the computer memory stuff, but we're about to go in deep
into that and we're going to talk about the different sections of memory inside the computer.
We're talking about virtual address spaces and in depth.
You know what's really happening when you load your program into memory and run it, you know?
So that's all I have to say about passed by reference and passed by value.
So let's go ahead and head back.
And I also wanted to let's see.
So this is just a slide of us heading over to check it out, which is what we did.
But this problem?
Let's see this problem that you solved.
Something that I kind of forgot is if you wanted to take a look at the table and kind of add up the
values for that.
You can whip out a calculator here.
And you can add these values together.
These are like this specific one was like a zebra.
I think it's one of the words I entered.
So let's let's go ahead and do that, actually, so I'm going to head back here.
It's kind of out of order, but I would like to kind of go back to this too and cover that.
So.
What I want to do is show you an example of this, so I think that we.
Had.
I'm just going to compile this just in case so
funky are so I'm going to call this funk.
So let's go ahead and run this.
So I'm jumping back kind of covered the last thing that I want to cover in the lecture, but I want
to go over this just so we can kind of double check and you can double check your work for this problem
because this was kind of like a big step forward with you having to solve this problem.
So I'm going to go ahead and enter some words here.
I'm going to enter car, I'm going to enter that and I'm going to enter Dad.
OK, so it said the string with the largest sum is back with a sum of three hundred and eleven.
So what I want to do now is head over to this ASCII table.
This is just on a Wikipedia page.
You can find another ASCII table if you like.
And I'm going to just about this handy dandy calculator.
And I want to show you.
Let's see if we can actually leave all this clear.
So I'm going to clear this stuff right here, so we have these other things that just kind of got saved
off here.
So we got this history, I don't really need the history, but let's go ahead and just let's go ahead
and just calculate our stuff.
So it said that.
That was the string with the largest sum, and that's somewhat 311, so now let's just prove that that
is actually true.
So right here, we have our lowercase characters, right?
So this is what I was talking about here.
This decimal values what we're interested in for the sum.
So I know I'm kind of jumping back here and probably that, but I want to go back over this, so.
We got bee right bees in 98, so we do 98, plus we got a 97, so that's 97 plus.
T is 116, 116 equals three, 11, right, and that's what it said, it said the sum was three, 11.
We have car and dad as well.
So let me go ahead and clear this car is 99 plus 97 for a plus.
114.
That's three, 10.
So that wasn't necessarily one last, that's pretty funny, right?
Then we have dad, right, so dad is just going to be one hundred plus ninety seven plus one hundred
thirty be two ninety seven.
Right.
So just wanted to show you that you can refer to this ASCII table if you are confused on that part.
You can go and look at these decimal values and you can kind of double check manually with a calculator.
What's going on with your program?
So with that, that is kind of the last thing that I wanted to go back and go over.
Hopefully you understand the references thing and.
Understood how to pass an array.
We're going to have another function's lecture, I think, where I just want to go over calling multiple
functions and also the fact that you can call a function within another function just like we've been
doing in main.
But imagine that we call Maine and then we're in Maine, we call, get some and then get some insight
inside here and say, I guess we call another function.
And that's possible.
We can definitely do that.
I'm also going to cover something else called prototyping.
So with that, I will see you in the next lectures.