Functions are the basic infrastructure of C plus plus code.
So the code is contained in functions.
To execute that, you have to call a function.
So C plus plus is remarkably flexible in the ways that you can define and call functions here.
So you can define functions with a fixed number or parameters or variable number of parameters.
So you can write generic code so that the same code can be used with different types.
And you can even write generic code with a variable number of types here.
So defining C plus plus functions.
So at the most basic level, a function has parameters here.
So it has the code to manipulate the parameters and it has the return value.
So C plus plus gives you several ways to determine these three aspects.
In this next lecture, we will cover these parts of C plus plus function from left to the right of declaration.
So functions can also be templated, but this will be left to later sections.
So a function must be defined exactly once, but should overloading.
You can have many functions with the same name that differentiate by their parameters.
So code that uses a function has to have access to the name of the function and so it needs to have
access.
So either the function definition or the declaration of the function also called the function prototype.
So the compiler uses the prototype to type check that the calling code is calling the function.
So using the right types here.
So typically libraries are implemented as separate compiled library files and the prototypes of the
library functions are provided in header files so that the many source files can use the functions by
including the heaters.
So however, if you know the function name parameters and return type, you can type the prototype yourself
in your file here.
So whichever you do, you are simply providing the information for the compiler to type check the expression
that calls function.
So it's up to the linker to locate the function in the library and either copy the code into the executable
or set up the infrastructure to use the function from a shared library.
So including the header file from for a library does not mean that you will be able to use the functions
from that library because in standard C plus plus the header file does not have information about the
library that contains a function.
So most of the C runtime library is implemented this way here.
So the function is compiled in a static library or a dynamic link library, and the function prototypes
are provided in a header file.
So you provide a library and the linker command line, and typically you will include the header file
for the library so that the function prototypes are available to the compiler.
So as long as the linker knows about the library, you can type the prototype in your code here.
So this can save you from including some large files into your source files.
The files that will mostly have prototypes of the function that you will not use here.
So far in this course, we have used one source files, so all the functions are defined in the same
file as where they are used.
So and we have defined the function before calling it.
So that is the function is defined above the code that calls it here.
So.
So here you do not have to define the function before it is used.
As long as the function prototype is defined before the function is called here.
So in main function we are going to print something here.
Moved here for example C out mult.
As you can see, we have to input the.
Okay.
Stay out here.
And as you can see, we got an error because we have to import the iostream header file.
And as you can see, we can now print something on the string.
So moved here instead.
And.
Six, six, seven.
And an line.
Here.
And after that, we're going to create the mold function here.
Mold here.
I will show you what's wrong with this code here.
Error is here.
And return the multiply by errors here.
So the mod function here defined after the main function here.
But this code will here.
Let's try this out.
Was actually said here.
Here.
This code will not compile because it is given the prototype before the main function.
So this is called here if we make it like that.
As you can see here, the function can compile and it will multiply six by seven here.
So the prototype doesn't have to be have two parameter names.
So this is because the compiler only needs to know the types of the parameters so their names.
So however, since parameter names should be self-documenting, it's usually a good idea to give the
parameter names so that you can see the purpose of the function here.
Now, here in this lecture, in this example, we the function is defined in the same source file.
So the internal linkage, this means the there is an internal linkage here in this code.
So if the function is defined in another file, the prototype will have external linkage.
And so the prototype will have to be defined, for example, like this here external.
Extend integer mode.
Here.
Integer.
Integer here.
So the extern keyword is one of many specifiers that you can add to a function declaration.
And in the previous lectures we have seen others.
So for example, the static specifier can be used on a prototype to indicate that the function has internal
linkage and the name can only be used in the current source file.
So current source file in this example here in for example, static.
Integer.
So you have to write static before the function return type here.
So in this example.
It is appropriate to mark the function as static.
In this case.
So you can also declare a function as external here which affects how the name of the function is stored
in the object file.
So this is important for libraries and will be covered shortly.