WEBVTT

00:01.860 --> 00:03.330
After the return type.

00:04.300 --> 00:08.710
Uh, the parameters, the function name and have been determined.

00:08.740 --> 00:12.430
You need to define the body of the parameters.

00:12.430 --> 00:15.680
So we're going to firstly make the return time.

00:15.700 --> 00:23.080
Then our function name, for example, my function, the parameters of this function is going to get,

00:23.080 --> 00:25.030
for example, my integer.

00:25.030 --> 00:28.660
And this is the function.

00:29.960 --> 00:31.070
Uh, body here.

00:31.070 --> 00:39.470
So the code for a function must appear between a pair of braces here, as you can see here.

00:39.470 --> 00:46.490
So if the function returns a value, then the function must have at least one line.

00:46.520 --> 00:51.800
The last line in the function with the return statement here.

00:52.790 --> 01:00.320
So this must return the appropriate type or type that can be implicitly converted to the return type

01:00.320 --> 01:01.430
of the function.

01:01.430 --> 01:09.500
So as mentioned before, if the function is declared as auto here, the compiler will deduce the return

01:09.500 --> 01:09.800
type.

01:09.800 --> 01:14.840
So in this case, the return statement must return the same type.

01:15.450 --> 01:21.630
Now, when a function is called, the compiler checks all the overloads of the function to find one

01:21.630 --> 01:24.150
that matches the parameter in the calling code.

01:24.150 --> 01:30.330
So if there is a no exact match, then the standard and user defined type conversions are performed.

01:30.330 --> 01:37.130
So the values provided by the calling code may be different type from parentheses here.

01:37.140 --> 01:38.760
So parameters.

01:39.210 --> 01:45.660
So by default parameters are passed by value and the copy is made.

01:45.660 --> 01:47.010
So which means that the.

01:47.920 --> 01:53.230
Parameters are treated as local variables in the function like that here.

01:53.230 --> 01:58.390
So you can't use the my integer variable outside the function here.

01:58.540 --> 02:04.240
These are the just local variables that you can use these variables inside the function.

02:04.240 --> 02:05.020
So.

02:07.200 --> 02:13.320
Uh, the variety of the function can decide to pass parameter by reference either through the pointer

02:13.320 --> 02:14.790
or a C plus plus reference.

02:14.790 --> 02:22.200
So pass by reference means that the variable in the calling code can be altered by the function, but

02:22.200 --> 02:27.150
this can controlled by making the parameters constant here.

02:27.150 --> 02:34.470
So in which case the reason for passing by reference is to prevent the potentially costly copying being

02:34.470 --> 02:35.040
made.

02:35.040 --> 02:41.880
So the built in arrays are always passed as a pointer to the first item to the array.

02:41.880 --> 02:47.250
Here, the compiler will create the temporaries when needed.

02:47.250 --> 02:51.750
So for example, when a parameter here, for example, let's create a void.

02:51.750 --> 02:59.760
If the when a parameter is a const reference and the calling code uh, passed pass as a literal, a

02:59.760 --> 03:04.890
temporary object is created here and it's only available to the function here.

03:05.100 --> 03:13.680
Let's create a function float and here we're going to make the F here.

03:15.850 --> 03:19.420
F 1.0, for example.

03:19.450 --> 03:20.740
Or double.

03:21.670 --> 03:22.510
Double d.

03:23.590 --> 03:25.780
D equals.

03:26.640 --> 03:27.620
2.0.

03:27.630 --> 03:29.880
As you know, this is the plot number.

03:29.880 --> 03:33.840
And here and we're going to.

03:34.750 --> 03:45.400
Add the F pass the D variable, the double variable to the our f function here which returns nothing.

03:45.400 --> 03:48.580
But we have a const parameter here.

03:49.330 --> 03:49.540
The.

03:51.240 --> 03:52.770
So here, this is.

03:52.770 --> 03:53.730
Okay.

03:53.820 --> 03:54.450
Okay.

03:54.450 --> 03:57.960
And temporary float is created.

03:58.260 --> 04:01.170
And this is actually the almost the same.

04:01.710 --> 04:03.600
And it's okay.

04:03.600 --> 04:10.950
And the temporary float is created here actually, in this.

04:10.950 --> 04:12.000
Not in this line.

04:12.000 --> 04:13.590
In this line we have, uh.

04:13.590 --> 04:14.280
Okay.

04:14.280 --> 04:22.440
And it code can compile successfully and in f when we pass 1.0 as parameter, the temporary float is

04:22.440 --> 04:22.830
created.

04:22.830 --> 04:31.950
And, um, when we're going to pass the D as a parameter, the temporary float is created as well here

04:31.950 --> 04:36.870
and we can pass the initializer list list in this functions here.

04:36.870 --> 04:39.960
So you can pass initializer list as a parameter.

04:39.960 --> 04:44.160
If that list can be converted to the type of the parameter.

04:44.160 --> 04:48.120
So for example, let's we're going to create another struct here.

04:48.150 --> 04:53.010
Actually, we can delete this codes, we can create another struct here.

04:53.010 --> 04:54.510
We can delete this as well.

04:55.930 --> 04:58.750
Uh, struct point here.

04:58.780 --> 05:02.080
This is going to take an integer x here.

05:02.200 --> 05:05.140
An integer A or integer.

05:06.280 --> 05:07.930
Integer B here.

05:09.470 --> 05:10.100
Here.

05:10.100 --> 05:15.980
So we're going to make the ender function named set point.

05:16.430 --> 05:19.550
And the point, as you can see, the point is a struct.

05:19.550 --> 05:23.480
We're going to take the point struct as a parameter.

05:24.510 --> 05:26.820
And name it here.

05:26.820 --> 05:31.280
So in the main function, we're going to code a point.

05:31.290 --> 05:42.330
We're going to create a point object named P and reach the P dot A equals one and P dot B equals one

05:42.330 --> 05:42.990
as well.

05:42.990 --> 05:45.720
And here we're going to set point.

05:45.750 --> 05:53.160
We're going to use the set point function and we're going to pass to pass P as a parameter.

05:53.640 --> 05:56.550
And we're going to set point here.

05:57.660 --> 06:00.220
Uh, using the curly braces here.

06:00.240 --> 06:02.970
One and one here.

06:03.960 --> 06:09.920
And we're going to return zero, as always in main function here.

06:09.930 --> 06:11.010
So.

06:12.600 --> 06:17.760
Uh, this code defines a structure that has two members.

06:17.910 --> 06:18.660
Here.

06:18.690 --> 06:20.520
Actually, let's make it look nice here.

06:20.520 --> 06:21.010
Like this.

06:21.030 --> 06:27.120
This structure has a two members, and in the main function, a new instance of point is created on

06:27.120 --> 06:32.400
the stack, and it is initialized by accessing their members directly.

06:32.400 --> 06:39.570
So the instance is then passed to a function that has point parameters as the parameter of set point

06:39.600 --> 06:40.080
here.

06:40.110 --> 06:42.390
Set point is passed by value.

06:42.420 --> 06:49.920
The compiler creates a copy of the structure on the stack of the function here, and the second call

06:49.920 --> 06:51.660
of the set point does the same.

06:51.660 --> 06:58.440
The compiler will create a temporary point object on the stack of the function and initialize it with

06:58.440 --> 07:01.320
the values in the initializer list.
