WEBVTT

00:02.250 --> 00:02.820
Hello, there.

00:02.910 --> 00:07.440
I'm going to start off this advanced C++ course by going over some background material.

00:08.190 --> 00:13.680
So this is to make sure that everyone understands the basics before we go onto the more advanced material.

00:14.430 --> 00:17.940
We're going to start off with local variables and function arguments.

00:20.170 --> 00:27.490
When we define a local variable, it's inside a scope, which means a pair of curly brackets or braces.

00:28.120 --> 00:29.480
So that's the start of the scope.

00:29.500 --> 00:30.790
That's the end of the scope.

00:31.990 --> 00:37.210
We define the variable inside the scope and then after that we can use it.

00:37.570 --> 00:42.100
So when the program reaches this point, it'll allocate memory on the program's stack.

00:42.610 --> 00:46.120
In this case, enough memory to hold an int, and it'll initialize it.

00:46.510 --> 00:48.880
If we give it an initial value, it'll use that.

00:49.330 --> 00:55.120
Otherwise, it'll use a default value. For a built-in type like int, it will use whatever data happens to

00:55.120 --> 00:56.290
be in the memory already.

00:57.190 --> 01:01.750
If it's an object of a class, it'll use the class's default constructor to initialize the object.

01:02.950 --> 01:07.330
And then when the program reaches the end of the scope, this variable will be destroyed, which

01:07.330 --> 01:13.000
means that the memory it was using will be released and the data in it is no longer accessible to the

01:13.000 --> 01:14.770
program - or to the programmer, anyway.

01:16.180 --> 01:21.190
So we say that i has gone out of scope or is not in scope or is no longer in scope.

01:26.650 --> 01:32.110
When we pass a variable to a function, if we don't do anything special, then this is going to be passed

01:32.110 --> 01:32.740
by value.

01:33.070 --> 01:39.460
So this means that there'll be a local value, y, inside this function; it'll be initialized from

01:39.520 --> 01:41.290
whatever variable we pass into it.

01:42.280 --> 01:48.250
So in this case, y is going to be an int with the same value as x, which is two. The return statement

01:48.250 --> 01:50.830
means that we copy y into the function's return value.

01:51.580 --> 01:54.280
So the function's return value will [be] two in this case.

01:54.610 --> 01:58.660
And then y gets destroyed at the end of the function. When the function returns,

01:59.020 --> 02:06.400
we can then copy the data from this returned value into another variable, and in this case, we're

02:06.400 --> 02:08.920
using it to initialize a new variable, z.

02:12.250 --> 02:13.480
So let's try this out.

02:13.720 --> 02:19.360
Here's a main function where we define the variaable x, then we call the function, passing x as its argument,

02:20.590 --> 02:26.860
and then we return y from the function, and we use that to initialize z. I'm going to print out the

02:26.860 --> 02:32.410
address of all these variables, using the address-of operator, so we can see if they're the same variable

02:32.410 --> 02:33.430
or different variables.

02:34.540 --> 02:35.260
So let's try that.

02:39.630 --> 02:44.490
So there we are, x, y and z all have different addresses, which means they're different objects.

02:46.110 --> 02:51.930
And then x still has the value two after we call the function and now z has the value two.

02:55.850 --> 03:00.860
Because these are all different variables, if we change the value of y it won't actually affect

03:00.860 --> 03:01.820
the value of x.

03:03.950 --> 03:05.150
And let's try that.

03:06.530 --> 03:07.190
So there we are.

03:07.400 --> 03:07.670
x is

03:07.940 --> 03:12.100
still two and z it will be the return value from this function, which is now.

03:12.110 --> 03:12.440
one.

03:17.960 --> 03:21.410
If we want to change the value of x, there's actually two ways we can do this.

03:21.950 --> 03:23.510
One is pass by address.

03:23.900 --> 03:29.120
So in this case, instead of passing the variable, which will cause it to be copied, we pass the address

03:29.120 --> 03:30.170
of the variable.

03:31.010 --> 03:32.990
And then the function needs to take a pointer.

03:33.890 --> 03:39.170
So this will now have a local variable y, which is a pointer to int, and the address is going

03:39.170 --> 03:40.850
to be the address of x.

03:41.660 --> 03:46.400
And then if we dereference that, that'll give us the data in that memory, which is also used by

03:46.400 --> 03:47.150
the variable x.

03:47.720 --> 03:51.320
So if we change that data to one, then x will have the value one.

03:56.900 --> 03:58.400
So here's the code again.

03:58.520 --> 04:03.170
I've now changed it, so we're passing the address of x and we're taking a pointer in func.

04:04.430 --> 04:06.770
So we now dereference y, so that equals one.

04:08.610 --> 04:10.290
So let's have a look at what we get.

04:13.400 --> 04:18.230
So the address of x is the same as the value of y, so y is a pointer.

04:18.860 --> 04:20.750
So they're both pointing to the same bit of memory.

04:21.320 --> 04:22.910
z is a different value, again.

04:25.580 --> 04:31.340
So z is equal to the return value, which is one, but because we've changed the value of x through

04:31.340 --> 04:34.430
this pointer, the value of X is now also one.

04:41.500 --> 04:45.910
And then the other way to change the address of x is to use pass by reference.

04:46.480 --> 04:50.140
So this means that the function now takes a reference instead of a pointer.

04:51.250 --> 04:54.670
So the local variable y in here is going to be a reference to x.

04:55.360 --> 04:58.660
So this means that y can access x's data.

04:59.230 --> 05:03.520
Usually, this is implemented as a pointer which automatically gets dereferenced by the compiler.

05:03.970 --> 05:06.850
So in effect, it's a simpler way of writing what we had before.

05:09.490 --> 05:13.000
So if we make these changes, so we now using a reference.

05:15.750 --> 05:18.770
And in fact, x and y have the same address.

05:18.810 --> 05:25.230
So in this case, the compiler has implemented the reference as a pointer to x. And we can see this

05:25.230 --> 05:27.300
x is one when the future returns.

05:30.120 --> 05:33.730
Then finally, one useful thing you can do is pass by const reference.

05:34.690 --> 05:39.160
So this means that the thing that's being referred to can't be changed.

05:40.090 --> 05:41.500
(So I just move that out of the way.)

05:44.800 --> 05:45.460
(Down here now!)

05:47.300 --> 05:53.560
So if you have a class object and your function only wants to inspect the object, so that's read-only

05:53.570 --> 05:58.910
access, then usually the most efficient way of doing that is to pass it by reference to const.

06:01.450 --> 06:06.100
Because you're not passing by value, that means you're not making a copy, so there isn't any overhead

06:06.100 --> 06:11.500
from copying the data, which can be quite expensive for some classes; it can involve a lot of operations

06:11.500 --> 06:16.210
which add to execution time. And because the reference is to a const,

06:16.510 --> 06:20.410
this means that this object here can't in fact be changed.

06:27.380 --> 06:29.300
So, in here, let's change this to const.

06:33.190 --> 06:38.350
So we get an error, which says you cannot assign to a variable that is constant.

06:42.570 --> 06:44.730
And that's because y is a reference to const.

06:45.150 --> 06:47.820
So we're not allowed to use y to modify x.

06:51.000 --> 06:54.410
And then if we remove that line, it does work.

06:56.940 --> 06:59.910
Okay, so as I say, it's all pretty basic stuff.

07:00.060 --> 07:05.280
Nothing in here should be a surprise, I hope. But it's useful to remind ourselves and perhaps also

07:05.280 --> 07:07.590
to get a better understanding of how these things work.

07:09.030 --> 07:10.630
Okay, so that's it for this video.

07:10.680 --> 07:11.520
I'll see you next time.

07:11.520 --> 07:13.680
But meanwhile, keep coding!