Types of Functions In C++

There are two types of functions namely:
 
Library Functions
 
The declaration of library function is in the header file specified at the beginning of the program.
 
The definition is in a library file that is automatically linked to the program.
 
Declaration and definition are not required. We will only call the function. Example: clrscr ( ).
 
User Defined Functions
 
Declaration and definition are part of the source file (*.cpp file).
 
Function definition and declaration have to be written by us.
 
Example of user defined function is the add function that we saw in the previous section.
 
Pass by Value and Pass by Reference
 
We have seen as to how to pass values to a function by making use of arguments. First let me make one concept clear.
 
What is a parameter and what is an argument? Parameter is the variable that you declare within the function. Argument is the variable that you pass to a function.
 
Pass By Value
 
Example:
 
void check ( int x )
{……………….
}
int main( )
{
……………….
int b=10;
check(b);
………….
}
 
In this function, x is a parameter and b ( which is the value to be passed ) is the argument.
 
In this case the value of b (i.e. the argument) is copied in x (i.e. the parameter). Hence the parameter is actually a copy of the argument.
 
The function will operate only on the copy and not on the original. This method is known as PASS BY VALUE. So far we have dealt only with pass by value.
 
 
Out put of the program
 
 
You can see that the value of a is unchanged. The function square works only on the parameter (i.e. on x ).
 
Pass By Reference
 
In pass by reference method, the function will operate on the original variable itself. It doesn’t work on a copy of the argument but works on the argument itself.
 
Consider the same square function example We took in the previous section.
 
 
Out put of the program
 
 
As you can see the result will be that the value of a is 100. The idea is simple: the argument passed is the address of a. The parameter of square is a pointer pointing to type integer. The address of a is assigned to this pointer. Within the function We’ve written:
 
*x = (*x) * (*x);
 
*x is pointer variable we write ‘*’ operator before any variable then it becomes a pointer variable.* when used before a pointer, will give the value stored at that particular address.
 
Hence we find the product of a and store it in a itself. i.e. the value of 100 is stored in the address of a instead of 10 which was originally stored.
 
This is a call-by-reference method which was used in C. In C++ there is a different approach. Of course you can use the above method, but C++ has its own way.
 
Pass by Reference C++ style
 
In C++ we make use of the reference parameter. All you need to do is put & before your function’s parameter. Example: void square (& x)
 
Whatever operation is done on x will actually affect the original calling argument. Basically x can be said to be an implicit pointer.
 
The difference is that you needn’t use *x to operate on the parameter. We’ll show you with an example (the same square function example).
 
 
Out put of the program
 
 
The line x = x * x ; actually operates on a and not on a copy of a.
 
Function Overloading
 
Many functions can have the same name but they should have different number of arguments or the types of the arguments should be different. This is known as function overloading.
 
Consider the example given below:
 
// A Program to illustrate function overloading
 
 
Out put of the program
 
 
In the above program, at the starting itself, the compiler can see that there are three functions with the name of display.
 
But the compiler is also clever enough to see the arguments of each of the three functions.
 
Once it sees that the arguments are different, the compiler will consider the three functions to be different.