WEBVTT

00:00.810 --> 00:05.100
Functions are the basic infrastructure of C plus plus code.

00:05.100 --> 00:08.040
So the code is contained in functions.

00:08.040 --> 00:11.400
To execute that, you have to call a function.

00:11.400 --> 00:17.070
So C plus plus is remarkably flexible in the ways that you can define and call functions here.

00:17.070 --> 00:23.570
So you can define functions with a fixed number or parameters or variable number of parameters.

00:23.580 --> 00:29.220
So you can write generic code so that the same code can be used with different types.

00:29.220 --> 00:33.810
And you can even write generic code with a variable number of types here.

00:33.810 --> 00:37.560
So defining C plus plus functions.

00:37.560 --> 00:41.700
So at the most basic level, a function has parameters here.

00:41.700 --> 00:47.730
So it has the code to manipulate the parameters and it has the return value.

00:47.730 --> 00:52.650
So C plus plus gives you several ways to determine these three aspects.

00:52.650 --> 01:02.170
In this next lecture, we will cover these parts of C plus plus function from left to the right of declaration.

01:02.170 --> 01:08.170
So functions can also be templated, but this will be left to later sections.

01:08.590 --> 01:14.380
So a function must be defined exactly once, but should overloading.

01:14.380 --> 01:20.590
You can have many functions with the same name that differentiate by their parameters.

01:20.590 --> 01:27.580
So code that uses a function has to have access to the name of the function and so it needs to have

01:27.580 --> 01:27.940
access.

01:27.940 --> 01:34.630
So either the function definition or the declaration of the function also called the function prototype.

01:34.630 --> 01:40.690
So the compiler uses the prototype to type check that the calling code is calling the function.

01:40.690 --> 01:42.430
So using the right types here.

01:42.430 --> 01:49.120
So typically libraries are implemented as separate compiled library files and the prototypes of the

01:49.120 --> 01:56.350
library functions are provided in header files so that the many source files can use the functions by

01:56.350 --> 01:57.820
including the heaters.

01:57.820 --> 02:04.690
So however, if you know the function name parameters and return type, you can type the prototype yourself

02:04.690 --> 02:06.250
in your file here.

02:06.250 --> 02:13.210
So whichever you do, you are simply providing the information for the compiler to type check the expression

02:13.210 --> 02:14.470
that calls function.

02:14.470 --> 02:21.130
So it's up to the linker to locate the function in the library and either copy the code into the executable

02:21.130 --> 02:25.630
or set up the infrastructure to use the function from a shared library.

02:25.630 --> 02:32.200
So including the header file from for a library does not mean that you will be able to use the functions

02:32.200 --> 02:39.040
from that library because in standard C plus plus the header file does not have information about the

02:39.040 --> 02:41.620
library that contains a function.

02:42.080 --> 02:45.830
So most of the C runtime library is implemented this way here.

02:45.830 --> 02:53.720
So the function is compiled in a static library or a dynamic link library, and the function prototypes

02:53.750 --> 02:56.210
are provided in a header file.

02:56.660 --> 03:03.140
So you provide a library and the linker command line, and typically you will include the header file

03:03.140 --> 03:08.840
for the library so that the function prototypes are available to the compiler.

03:08.990 --> 03:15.530
So as long as the linker knows about the library, you can type the prototype in your code here.

03:15.530 --> 03:20.940
So this can save you from including some large files into your source files.

03:20.960 --> 03:26.810
The files that will mostly have prototypes of the function that you will not use here.

03:26.810 --> 03:33.440
So far in this course, we have used one source files, so all the functions are defined in the same

03:33.440 --> 03:35.690
file as where they are used.

03:35.690 --> 03:38.480
So and we have defined the function before calling it.

03:38.480 --> 03:44.190
So that is the function is defined above the code that calls it here.

03:44.190 --> 03:44.380
So.

03:44.430 --> 03:48.570
So here you do not have to define the function before it is used.

03:48.570 --> 03:53.220
As long as the function prototype is defined before the function is called here.

03:53.220 --> 03:55.740
So in main function we are going to print something here.

03:56.010 --> 04:01.170
Moved here for example C out mult.

04:02.280 --> 04:04.500
As you can see, we have to input the.

04:07.120 --> 04:07.870
Okay.

04:08.380 --> 04:09.640
Stay out here.

04:10.510 --> 04:16.960
And as you can see, we got an error because we have to import the iostream header file.

04:17.110 --> 04:20.070
And as you can see, we can now print something on the string.

04:20.080 --> 04:22.360
So moved here instead.

04:23.410 --> 04:24.340
And.

04:25.260 --> 04:27.660
Six, six, seven.

04:29.040 --> 04:30.150
And an line.

04:33.680 --> 04:34.190
Here.

04:34.190 --> 04:38.210
And after that, we're going to create the mold function here.

04:38.360 --> 04:40.100
Mold here.

04:40.460 --> 04:43.160
I will show you what's wrong with this code here.

04:43.870 --> 04:45.290
Error is here.

04:45.290 --> 04:50.750
And return the multiply by errors here.

04:52.100 --> 04:57.080
So the mod function here defined after the main function here.

04:57.080 --> 05:00.290
But this code will here.

05:00.320 --> 05:01.910
Let's try this out.

05:02.840 --> 05:04.940
Was actually said here.

05:10.350 --> 05:10.880
Here.

05:10.890 --> 05:18.150
This code will not compile because it is given the prototype before the main function.

05:18.150 --> 05:21.810
So this is called here if we make it like that.

05:25.330 --> 05:31.660
As you can see here, the function can compile and it will multiply six by seven here.

05:31.690 --> 05:36.740
So the prototype doesn't have to be have two parameter names.

05:36.760 --> 05:42.720
So this is because the compiler only needs to know the types of the parameters so their names.

05:42.730 --> 05:48.310
So however, since parameter names should be self-documenting, it's usually a good idea to give the

05:48.310 --> 05:54.370
parameter names so that you can see the purpose of the function here.

05:55.350 --> 06:03.780
Now, here in this lecture, in this example, we the function is defined in the same source file.

06:03.780 --> 06:09.350
So the internal linkage, this means the there is an internal linkage here in this code.

06:09.360 --> 06:15.840
So if the function is defined in another file, the prototype will have external linkage.

06:15.840 --> 06:24.390
And so the prototype will have to be defined, for example, like this here external.

06:25.830 --> 06:28.340
Extend integer mode.

06:29.200 --> 06:29.640
Here.

06:29.650 --> 06:30.520
Integer.

06:30.550 --> 06:31.630
Integer here.

06:34.350 --> 06:41.430
So the extern keyword is one of many specifiers that you can add to a function declaration.

06:41.430 --> 06:45.950
And in the previous lectures we have seen others.

06:45.960 --> 06:51.780
So for example, the static specifier can be used on a prototype to indicate that the function has internal

06:51.780 --> 06:56.760
linkage and the name can only be used in the current source file.

06:57.330 --> 07:05.280
So current source file in this example here in for example, static.

07:06.710 --> 07:07.340
Integer.

07:08.140 --> 07:11.530
So you have to write static before the function return type here.

07:11.740 --> 07:13.450
So in this example.

07:14.640 --> 07:18.730
It is appropriate to mark the function as static.

07:18.750 --> 07:19.530
In this case.

07:19.530 --> 07:27.390
So you can also declare a function as external here which affects how the name of the function is stored

07:27.390 --> 07:30.420
in the object file.

07:30.420 --> 07:35.370
So this is important for libraries and will be covered shortly.
