In this video we'll learn about passing arrays to c++ functions.
We can pass arrays into functions by providing the square brackets
in the parameter information.
So in this case, we can have a function named print array
that expects an array of integers and returns nothing.
The idea is that this function iterates through the array
and displays the array values.
Pretty handy function, right.
But arrays are different in c++.
Remember that an array name evaluates to the address or location and memory
of the first element of the array.
In other words, the beginning of the array.
So what's being passed into the function is not a copy of the
entire array, but only the address of the first element of the array.
That means that the function has no idea how many elements are in the array.
So the programmer doesn't know how many times to iterate.
So when we pass arrays to functions, we also need to pass in the size
of the array, so that we now know how many times to iterate.
Let me show you an example.
In this example, you can see that both the function prototype
and the function definition have square brackets after
the formal parameter name.
This tells the compiler that the function expects an array
of integers in this case.
So far so good.
Then in main, I have an array of five integers called my
numbers, and it's been initialized to the integers 1 through 5.
So I call the print array function and pass in the array my numbers.
All good so far.
Now I execute the print array function.
But I have no idea how many times I need to iterate.
Since the array name has no size information, I'm stuck.
If the array has a sentinel value, like a c-style string,
then I can iterate until I see that sentinel value.
But in this case, the my numbers array has no such sentinel value.
There's no way I can write this function body in a way that it will
work with any array of integers.
So the solution is to pass in the size of the array to the function as well.
Let's do that next.
Now we've added a size parameter to both the function prototype
and the function definition.
In the main, when I call the function, I pass in my numbers and the size of
the array which is 5 in this case.
Now when i execute the body of the print array function, I know
the location of the array which is the array name and I know how
many elements are in the array.
I can easily write a for loop now that iterates through the
array and displays every integer.
This looks great but there is one potential gotcha that
you need to be aware of.
Since we're passing into the function, the location of the actual array
that was declared in main that means that we can modify that actual
array from within the function.
This could be useful in the case of a function like zero array that
we can call whenever we want to 0 out all the elements in an array.
However, in the print array function, we don't want to modify the array.
If we do it's probably an unintentional error.
We'll see how to protect ourselves from this kind of error in the
next slide, but first let's see how we could write a useful
function like zero array.
In this case the zero array function receives the location of the array
and the size of the array.
We could simply iterate through the array and set
each array element to zero.
Notice that when we print the array in main after we call zero array,
all the array elements are now zero.
So let's get back to see what we can do to have the compiler
help us, so we don't change an array we don't want to.
We can define function parameters as const parameters.
This tells the compiler that these parameters are read-only within the function body.
Any attempt to modify them will result in a compiler error.
So in this case notice that we included the const
keyword right before the array parameter declaration.
That's it.
Now if I try to modify any array element as i do in
the assignment statement, a compiler error will occur.
Depending on your compiler, the error message will say something like error
trying to assign to a read-only value.
The idea of passing the location of a variable to a function
instead of the value of a variable is fundamental in understanding
the next topic we'll talk about, it's called pass by reference.
Before we talk about pass by reference, I want to be sure that
you really understand the whole idea of passing arrays to functions.
So I'm in the IDE and I'm in the section 11 workspace, the
arrays and functions project.
And let me show you what I've got here.
Let's start at the main.
I've got a real simple array here, array of integers called my scores,
and I've initialized it to 100 98 90 86 and 84, right 5 integers.
Now what I want to do is i want to call a function called print
array, and i want to pass into that function the array and the size
of the array, as we saw before.
Perfect.
That should print out 100 98 90 86 and 84.
Now I've got a function called set array.
And what this function will do is it'll set every
value of that array to 100.
So when i come back, it'll print out 100 five times, so that
will actually modify the array.
So let's see what these functions look like.
They're pretty straightforward.
There are the function prototypes.
That's the function prototype for print array.
It expects an array of integers and the size of the array.
The set array function expects an array of integers, the size of the
array and the value that we want to set each one of those elements to.
All right, so let's look at the print array and I put const
here because it's something that we need to think about.
Whenever we write functions that work with arrays.
So let's look at this.
What do we do here?
Well, we iterate.
We iterate for I equal to 0.
While I is less than size, that's the the idiom that we do all
the time when we're working with arrays, and we're incrementing by 1.
And then all I'm doing is I'm outputting array sub-I.
So I'm going to go from array sub-0, array sub-1, array sub-2, all the
way to array sub-4, in this case.
And I'm going to print out each one of those integers.
Then I'm just going to print a new line at the end.
That will print out 100 98 90 86 and 84.
Perfect. So that's that line right here.
Then I'm going to call set array, and I'm going to pass
in, again, the array, the 5, which is the size and 100.
In this case, rather than doing any outputting, what I want to do is
I want to loop through that array again, so the same loop from I
equals 0, I less than size by one.
What I want to do at each iteration of the loop is I
want to set arr sub-I, right.
The Ith element of that array to the value that was passed in.
So in this case, I'm modifying all five of the array integers to
100, then I'm printing it again.
So what I expect from this program is the first print array statement is
going to print 100 98 90 86 and 84.
Then the last print array statement here is going to print five 100s.
Okay, so let's run this, and then we'll walk through this in detail so
you can see exactly what's going on.
There you go 100 98 90 86 84, and then 500, exactly what we expected.
So let's walk through this.
Let's start with the print array function first.
And my main has that array right here called my scores.
And I'm going to draw this visually, so you can see
exactly what's going on.
There's my scores.
It's an array of five integers.
Okay, and I'll put them in here: 100 98 90 86 and 84.
The value of the name of an array is the location of that
first element in the array.
So let's say that that integer 100 here is at location 2000.
And I'm just making that up, it could be any location.
That's 2000.
So whenever the c++ compiler sees an array name, this
is what it's going at.
In this case, my scores will evaluate to 2000.
Okay, perfect.
So now I'm going to call print array right here, this is what's going on.
So now I'm in my print array function.
And print array has several local variables.
It's got array, it's got size, and it's got this I right there.
Let's just worry about the array and the size variables right now.
So there's my arr, I'll just call it array.
And here's my size.
When the function is called, the values are copied.
So my scores is 2000, it's not the entire array, it's 2000.
So 2000 gets copied over here.
Size is the 5.
So size gets the 5, right.
Now I start looping.
And the first thing I do is it says I'm -- basically, I is zero right.
So what I'm going to do is I'm going to cout arr sub-0.
Well, what does that mean?
That means the first integer in the array.
Well, where's the array, right here.
That's that guy, right there.
So you can see that this print array function is referring to
the actual array in main because the array was never copied.
Instead, the address of the array was passed in.
It's going to go through each one of these through the loop.
It's going to print out arr sub-1, which is that guy right there 98.
So it's going to display them all.
Okay, that makes sense.
I hope so.
So so again, this will print out right here, it'll display 100 98,
just like we saw, 90 86 and 84.
Now let's look at this function call right here.
I still have my my scores array and it's still the same.
I haven't changed it.
And let me put those numbers back in here.
I'll put 100 in here 98 90 86 and 84, right.
Now I call set array.
Well, set array is over here, and it's got same idea, right.
It's got the arr variable, the size and the value.
So it's got arr, it's got size and it's got value.
Let's again assume that this is address 2000 right
here that's that name.
So in this case, I'm passing in the 2000, I'm passing in 5 for
size and I'm passing in the 100 for the value, and then I'm looping.
And notice that loop.
Every time in the loop, this is what I'm doing right here.
So the first time through I'm saying arr sub-0 is value which is 100.
It's always going to be 100.
Well, where is arr sub-0, it's at 2000.
So what i just did was I just came in here and put 100 in here.
Then I'm going to increment I and it becomes arr sub-1 equals 100.
So this guy is over here.
I just got rid of that one.
And what I'm doing is I'm initializing the entire array
to whatever value was passed in, which is 100 in this case, right.
When this function is finished, it's gone.
And what I'm left with is the updated array.
So this will print out 5 100s as you saw.
Okay, so hopefully, that makes sense.
It's a real different from pass by value.
But when you think about it, it is still doing pass by value because
it's copying the value of 2000, right.
It's not copying the entire array but it's copying 2000,
which is what my score is defaults to, that's its value.
All right, so one last thing.
We really don't want to be able to change that array in the print array.
Let's say that we did something like this something really silly
we could say arr sub-0 is 10,000.
Well, you know what let's see 50,000.
Just so that we could really see the difference.
Okay, this is probably a bug.
Okay, print functions don't tend to change anything.
They just display information.
But let's just say that we've got some bug in our code.
Now right here, I'll call print array again.
And I'll call it exactly the same with my scores and 5.
So look what happened here.
I called print array here.
It printed out the 5 100s.
But then it changed one of them, the first one to 50,000.
And then I'm calling it again.
So I'm expecting to see that first, there it is.
You see it.
That first array element right there just got changed to 50,000.
How do i prevent that.
Real easy way to prevent that is to come up to your print array
math function right here, and say that this is a constant array.
And I can do the same thing in the definition.
Now if you try to compile this, there's the error assignment
of read-only location arr.
So in other words, this function now print array cannot change array.
That's really what we want.
So we can comment that out.
And now we'll run just fine.
Oka, notice that's exactly what we want.
So when you're writing functions and they're passing around objects,
especially arrays and vectors and things like that, especially when
we talk about pass by reference in the next section, you've got to be
really aware of what can change what.
And if you do have access to the actual and you don't want to
change it, const is the way to go.
Okay, we'll talk more about that when we talk about pass by reference.