WEBVTT

00:00.720 --> 00:07.440
We said before that there are two different ways you can pass arguments to a function you can pass an

00:07.440 --> 00:11.570
argument by value which means you're copying the value into the function.

00:11.610 --> 00:18.600
So the argument isn't the same as the one he passed in or you can pass it by reference which means you're

00:18.600 --> 00:26.300
passing the address when you pass an array to a function you're always passing it by reference.

00:26.370 --> 00:32.840
So if the function changes the array in any way you will change it in the caller as well.

00:32.910 --> 00:35.520
Let's see an example of this.

00:35.520 --> 00:39.010
So say we had a function

00:42.940 --> 00:45.000
pass it or a

00:48.540 --> 00:51.230
this in we passed the link

00:56.480 --> 00:57.190
meant it.

00:57.210 --> 01:09.020
It's and all this will do is who's going to go through each element and increment it by one.

01:09.840 --> 01:10.470
This

01:21.020 --> 01:22.460
that's all it will do.

01:23.690 --> 01:26.550
And here we need to fine our array

01:34.870 --> 01:35.490
re

01:38.940 --> 01:42.310
9.

01:42.340 --> 01:47.620
And notice that we didn't set the link of the array.

01:47.680 --> 01:54.640
That's because it'll actually determine it by how many values that you initialized it to.

01:54.640 --> 01:57.750
So in this case the array length will be 5.

01:57.910 --> 02:04.810
And that's another way to initialize an array.

02:04.920 --> 02:09.170
Soooo we'll just call your function and pass it the array.

02:09.310 --> 02:17.880
And that's five in we'll just output the results.

02:19.850 --> 02:20.520
It's

02:28.760 --> 02:30.290
free of

02:37.550 --> 02:37.990
this

02:41.000 --> 02:41.540
this

02:45.090 --> 02:45.530
k.

02:45.550 --> 02:46.840
So if you run this

02:49.540 --> 03:01.110
you see that we change the caller's array and we did that by adding 1 and so notice that I'm passing

03:01.110 --> 03:05.350
in the length of the array to the function.

03:05.400 --> 03:09.060
That's because you always need to know how long the array is.

03:09.060 --> 03:19.370
Remember that we never want to go outside the array indices k so we can see by the output that we've

03:19.370 --> 03:23.290
actually changed are these values using the function.

03:23.300 --> 03:28.490
Remember the last lecture that I said that arrays and counters are similar.

03:28.940 --> 03:32.240
Well this is just one more similarity.

03:32.270 --> 03:40.680
It's kind of like we Pat we are passing in a constant integer pointer to the function since you can

03:40.770 --> 03:46.300
change the values of the array but not where it points to.

03:46.310 --> 03:52.140
This is a very important attribute of arrays and will want to use this quite a bit of course.

03:52.160 --> 03:59.450
If you don't want to be able to change the values of array you can always pass it using the Konski word.

03:59.450 --> 04:03.520
So let's say we didn't want to change it we just want to look at it.

04:03.830 --> 04:06.440
Then you do something like this.

04:08.960 --> 04:15.860
So the two important takeaways from this lecture is that arrays are always passed by reference and you

04:15.860 --> 04:20.960
should always passed the length of the array to a function.

04:20.990 --> 04:22.330
So we'll see.

04:22.370 --> 04:26.250
Well take a look at C strings in the next lecture.
