C++ Function Prototype (declaration)
C++ Function Prototype (declaration)
If a user - defined function is defined after main () function, compiler will show error. It is because compiler is unaware of user-defines function, types of argument passed to function and return type.
In C++, the function prototype is a declaration of function without its body to give compiler information about the user-defined function. Function prototype in the above example is :
int add(int , int);
You can see that, there is no body of the function in prototype. Also, there are only return type of arguments but no arguments. You can also declare function prototype as below but it's not necessary to write arguments.
int add(int a, int b);
Note: It is not necessary to define prototype if the user- defined function exixts before main() function.
Function Call
To execute the codes of function body, the user defined function needs to be invoked (called).
In the above program, add(num1, num2); inside main () function calls the user-defined function.
The function returns an integer which is stored in variable add.
Function Defination
The function itself is referred as function definition. Function definition in the above program is :
When the function is called, control is transfered to the first statement of the function body.
Then, other statements in function body are executed sequentially.
When all codes insides function is executed , control of program moves to the calling program.
Passing Arguments to Function
In programming , argument (parameter) refer to the data which is passed to a function (function definition) while calling it.
In the above example, two variables, num1 and num2 are passed to function during function call. these arguments are known as actual arguments.
The value of num1 and num2 are initilized to variables a and b respectively. These arguments a and b called formal arguments.
This is demonstrated in figure below :
Notes on passing arguments
The numbers of actual arguments and formals arguments and formals argument should be the same . (Exception: Function Overloading)
The type of first actual argument should match the type of first formal argument. Similarly, type of second actual argument should match the type of second formal argument and so on.
You may call function a without passing any argument. The number (s) of argument passed to a function depends on how programmer want to solve the program.
You may assign default values to the argument. These arguments are known as default arguments.
In the above program, both arguments are of int type. But it's not necessary to ahve both arguments of same type.
Return Statement
A function can return a single value to the calling program using return statement.
In the above program, the value of add returned from user-defined function to the calling program using statement below:
return add;
The figur below demonstrates the working of return statement.
In the above program, the value of add inside user-defined function is returned to the calling function. The value is then stored to a variable sum.
Notice that the variable returned, i.e., add is of type int and sum is also of int type.
Also, notice that the return type of a function is defined in function declarator int add (int a, int b) . The int before add (int a, int b ) means the function should return value of type int.
If no Value is returned to thr calling function then, void should be used.