WEBVTT

00:00.750 --> 00:11.970
So we saw before we could pass in values to functions so you could have something like this function

00:12.870 --> 00:18.100
and say it takes in an integer right.

00:18.510 --> 00:31.240
And if you define an integer mean say call it with the function pass in the X and say it's it's print.

00:31.240 --> 00:33.100
Ouch.

00:33.370 --> 00:35.500
Value x.

00:35.650 --> 00:40.190
It's here it's this.

00:40.660 --> 00:44.730
And we define the function down here.

00:48.030 --> 00:51.970
Say we can actually change the value right.

00:52.310 --> 01:09.830
If we wanted to know why it's us and we're in this C nothing surprising the value of y is gain and the

01:09.830 --> 01:11.870
value of x is 5.

01:12.230 --> 01:21.740
So we see that if we pass in the value of x into this function get change that value but it wouldn't

01:21.740 --> 01:25.340
change the original collar's variable at all.

01:25.340 --> 01:25.710
Right.

01:25.790 --> 01:27.410
So we're changing it to 15.

01:27.410 --> 01:29.470
But that doesn't affect X at all.

01:30.140 --> 01:33.920
And this is called pass by value.

01:34.100 --> 01:44.030
So we're just kind of copying the value into this function and just giving it in this case 5 say but

01:44.300 --> 01:50.690
actually have pointers here as are our parameters.

01:52.410 --> 01:54.290
Change it to be a pointer.

01:54.290 --> 01:57.590
Why do you take the address of X right.

01:57.630 --> 02:00.320
Because we have to pass in the address.

02:00.320 --> 02:04.490
This is remember that pointers taken addresses right.

02:04.540 --> 02:06.400
That's their values.

02:06.400 --> 02:12.500
And if we change all this what do you think the outcome should be.

02:13.380 --> 02:17.920
Let's try and see as your reference

02:22.260 --> 02:27.660
so you can see that the value of y is 15 and the value of x is 15.

02:27.660 --> 02:28.850
So what's going on.

02:29.110 --> 02:38.220
Well since we're passing the address of x K and again we're passing the address here we're changing

02:38.220 --> 02:40.410
x.

02:40.720 --> 02:45.530
This changes X because now y points to x.

02:45.550 --> 02:50.120
So why can change x's value right.

02:50.200 --> 02:52.390
So that's why it's changing here.

02:53.210 --> 02:56.800
So this is called passed by reference.

02:56.840 --> 03:03.070
So we're passing a reference to X's address here.

03:04.020 --> 03:04.600
OK.

03:04.980 --> 03:10.220
So this is one of the powerful features of pointers.

03:10.360 --> 03:15.720
This is why we use them so much as this is one of the good reasons for it.

03:15.880 --> 03:24.510
We can also do this with references to change this reference this.

03:24.670 --> 03:34.040
Now we don't need this right or that this k should see the same thing.

03:34.260 --> 03:37.260
Q Why is 15 in the value of X mean.

03:37.750 --> 03:44.500
Just so you totally believe me it's rere and this was a different value.

03:44.800 --> 03:54.900
Now it's 20 k so I'll often use references as per function parameters.

03:54.900 --> 04:04.460
Actually that's one of the primary uses I think of references is do you have it as a parameter.

04:04.470 --> 04:11.170
Often not for simple values like integers because usually you just want it to be just the value.

04:11.190 --> 04:19.050
So that's fine but later on you'll see that we really want to use references for a very specific reason.

04:19.350 --> 04:23.780
And I'll get into that in this in the future section.
