Function Prototype In C++
The function prototypes are used to tell the compiler about the
number of arguments and about the required datatypes of a function parameter,
it also tells about the return type of the function. By this information, the
compiler cross-checks the function signatures before calling it. If the
function prototypes are not mentioned, then the program may be compiled with
some warnings, and sometimes generate some strange output.
The compiler does not find what is the function and what is its
signature. In that case, we need to function prototypes. If the function is
defined before then we do not need prototypes.
Fig:Function Prototype In C++ |
Program Without Prototype
#include<iostream.h>
main()
{
function(50);
}
void
function(int x);
{
cout<<”the
value of x is : “<<x;
}
Output:
[warning]
function should return type
[warning]
function should have prototype
Program With Prototype
#include<iostream.h>
void
function(int); //prototype
main()
{
function(50);
}
void
function(int x);
{
cout<<”the
value of x is : “<<x;
}
Output:
The
value of x is:50
No comments:
Post a Comment