We said before that there are two different ways you can pass arguments to a function you can pass an
argument by value which means you're copying the value into the function.
So the argument isn't the same as the one he passed in or you can pass it by reference which means you're
passing the address when you pass an array to a function you're always passing it by reference.
So if the function changes the array in any way you will change it in the caller as well.
Let's see an example of this.
So say we had a function
pass it or a
this in we passed the link
meant it.
It's and all this will do is who's going to go through each element and increment it by one.
This
that's all it will do.
And here we need to fine our array
re
9.
And notice that we didn't set the link of the array.
That's because it'll actually determine it by how many values that you initialized it to.
So in this case the array length will be 5.
And that's another way to initialize an array.
Soooo we'll just call your function and pass it the array.
And that's five in we'll just output the results.
It's
free of
this
k.
So if you run this
you see that we change the caller's array and we did that by adding 1 and so notice that I'm passing
in the length of the array to the function.
That's because you always need to know how long the array is.
Remember that we never want to go outside the array indices k so we can see by the output that we've
actually changed are these values using the function.
Remember the last lecture that I said that arrays and counters are similar.
Well this is just one more similarity.
It's kind of like we Pat we are passing in a constant integer pointer to the function since you can
change the values of the array but not where it points to.
This is a very important attribute of arrays and will want to use this quite a bit of course.
If you don't want to be able to change the values of array you can always pass it using the Konski word.
So let's say we didn't want to change it we just want to look at it.
Then you do something like this.
So the two important takeaways from this lecture is that arrays are always passed by reference and you
should always passed the length of the array to a function.
So we'll see.
Well take a look at C strings in the next lecture.