WEBVTT

00:00.760 --> 00:08.290
In general function names have the same rules for variables, so they must begin with a letter or an

00:08.290 --> 00:13.800
underscore and cannot contain spaces or other punctuation characters here.

00:13.810 --> 00:17.410
So now I'm going to write some functions.

00:18.580 --> 00:27.550
Integer My function here, an integer like my function.

00:29.360 --> 00:33.070
And when we do the integer it here.

00:34.310 --> 00:45.200
This is the illegal and the compiler compiler will or will not compile here will not compile.

00:46.470 --> 00:46.890
Here.

00:48.390 --> 00:49.200
And.

00:50.830 --> 00:58.570
Uh, as I said earlier, they must begin with a letter or an underscore character and cannot contain

00:58.570 --> 01:05.470
spaces, as you can see here or other punctuation characters, for example, like dot comma here, they

01:05.470 --> 01:09.360
cannot contain any punctuation characters for.

01:09.370 --> 01:16.360
So following the general principle of Self-documenting code, you should name the function according

01:16.390 --> 01:18.580
to what it does.

01:18.580 --> 01:25.420
So there is one exception, and these are the special functions used to provide overloads for operators,

01:25.570 --> 01:27.880
which is mostly punctuation symbols.

01:27.880 --> 01:36.310
So these functions have a name in the form here, for example, the multi-year where the.

01:37.600 --> 01:43.300
Mold is like describing the function which will will multiply the two values one by one.

01:43.300 --> 01:46.690
So operators are one example of overloading here.

01:46.690 --> 01:53.950
So you can overload any function that is use the same name, but provide implementations with different

01:53.950 --> 01:57.120
parameter types or different parameter numbers.

01:57.130 --> 02:01.210
We can make it like that, for example, integer multi-year.

02:01.240 --> 02:03.020
We can make multiply.

02:03.220 --> 02:10.390
We we can multiply three numbers one by one here, for example, l h s integer r h s.

02:10.420 --> 02:15.040
If you make it like that or just make it integer, for example.

02:16.860 --> 02:19.100
Actually they will return the same value.

02:19.110 --> 02:22.470
This is just a different writing in C plus plus here.

02:22.470 --> 02:26.190
As you can see, we're going to get error because they are the two same function.

02:26.190 --> 02:30.600
As you can see the redefinition of multi year, multi, multi year.

02:30.600 --> 02:38.760
So if we make add another parameter integer x x, we're going to make the overloading function here

02:38.760 --> 02:47.460
and we're going to return the return, the multiply by an error and multiply in multiply it by the X

02:47.460 --> 02:49.380
age s here.

02:49.380 --> 02:55.560
And if you make it like that as standard C out.

02:57.040 --> 02:59.620
See out here and here.

02:59.620 --> 03:01.390
The most we're going to multiply.

03:01.390 --> 03:08.400
We're going to call this here 8 or 910.

03:08.410 --> 03:18.820
So in this case, uh, the multiply function will, uh, this code here, this code will call this function

03:18.820 --> 03:22.900
because this takes parameters and we gave this function two parameters.

03:22.900 --> 03:29.830
If we make it like that, uh, this is going to call the, uh, the third function.

03:29.920 --> 03:35.350
The second function here that we have three parameters and we are calling this function.

03:35.350 --> 03:39.310
So, and this named the function overloading here.

03:40.070 --> 03:45.050
As you can see here, we multiplied the three numbers one by one here.

03:45.050 --> 03:45.710
So.

03:47.140 --> 03:48.850
Uh, we have function parameters.

03:48.850 --> 03:53.500
The function here, they may not have any parameters like this here.

03:53.500 --> 03:58.750
For example, we can create a function with that parameters like this.

03:58.750 --> 04:05.170
But they let's return create without parameters, but we'll have the return value.

04:05.170 --> 04:14.740
So in which case the case is the function is the fate defined with a pair of empty parentheses.

04:15.130 --> 04:17.110
Uh, like this here, for example.

04:17.110 --> 04:18.370
So a function.

04:18.580 --> 04:26.170
The function definition must give the type and name of the parentheses parameters between parentheses

04:26.170 --> 04:27.070
like this here.

04:27.070 --> 04:28.090
So.

04:29.280 --> 04:34.500
And, uh, in many cases the functions will have a fixed number of parameters.

04:34.500 --> 04:37.800
But you can write functions with a variable number of parameters.

04:37.800 --> 04:42.610
So you can also define functions with the default values or some of the parentheses.

04:42.630 --> 04:45.150
So some of the parameters.

04:45.150 --> 04:52.110
So in effect a providing a function that overloads itself on the number of parameters parameters passed

04:52.110 --> 04:53.550
to the function.

04:53.550 --> 05:00.090
So variable argument list and default arguments will be covered later in this course.

05:00.090 --> 05:07.950
Here we can also make the make the specify the exceptions in C plus plus function here.

05:07.950 --> 05:14.010
The functions also can be marked to indicate whether they will throw an exception.

05:14.010 --> 05:18.570
So more details about exceptions will be given in next lecture.

05:18.570 --> 05:22.410
But there are two syntaxes you need to be aware of.

05:22.440 --> 05:29.170
So earlier versions of C plus plus allowed you to use the trough specifier.

05:30.350 --> 05:34.320
Uh, specified on the function in three ways.

05:35.210 --> 05:43.190
Firstly, you provide a comma separated list of types of the exceptions that may be thrown by code in

05:43.190 --> 05:44.150
the function here.

05:44.870 --> 05:49.040
The second you can provide an ellipsis like this.

05:49.520 --> 05:50.240
Here.

05:53.000 --> 05:58.580
Lapses or which means that the function may throw an exception.

05:58.580 --> 06:06.050
And thirdly, you can provide an empty pair of parentheses, which means that the function will not

06:06.050 --> 06:07.220
throw an exception.

06:07.220 --> 06:08.840
So the syntax here.

06:09.110 --> 06:13.700
Throwing exceptions to exceptions looks like this.

06:13.700 --> 06:19.220
For example, throw we're going to specify the exceptions.

06:19.220 --> 06:23.930
For example overflow overflow error and.

06:25.320 --> 06:25.770
Do.

06:25.800 --> 06:30.930
We're going to make, for example, do something which potentially.

06:32.550 --> 06:34.980
May overflow flow.

06:35.190 --> 06:40.290
So the trough specifier has been deprecated in C plus plus 11.

06:40.290 --> 06:47.430
As you can see, the compiler will say the dynamic exception specifier, uh, overflow error is deprecated.

06:47.430 --> 06:50.700
Consider using noexcept files instead.

06:50.700 --> 06:58.020
Here we can change ignore accept files so it throws specifier has been deprecated in C plus.

06:58.020 --> 07:03.660
Plus is largely because the ability to indicate that the type of function was not useful.

07:03.660 --> 07:10.260
So however, the version of throw that indicates that no exception will be thrown was found to be useful

07:10.290 --> 07:18.690
because it enables the compiler to optimize code by providing no code infrastructure to handle exceptions

07:18.690 --> 07:19.950
here, exceptions here.

07:19.950 --> 07:29.160
So the C plus plus 11 retains this behavior with no accept specifier and the no accept specifier.

07:29.160 --> 07:34.930
Here we can delete this false here and add with no accept specifier.

07:34.930 --> 07:36.220
We're going to check.

07:37.280 --> 07:44.720
Check the parameter and handle overflow appropriately.

07:47.380 --> 07:48.010
Here.
