What are function prototypes?
In C++ all functions must be declared before they are used. This is accomplished using function prototype. Prototypes enable complier to provide stronger type checking. When prototype is used, the compiler can find and report any illegal type conversions between the type of arguments used to call a function and the type definition of its parameters. It can also find the difference between the no of arguments used to call a function and the number of parameters in the function. Thus function prototypes help us trap bugs before they occur. In addition, they help verify that your program is working correctly by not allowing functions to be called with mismatched arguments.
- A general function prototype looks like following :
return_type func_name(type param_name1, type param_name2, …,type param_nameN);
- The type indicates data type. parameter names are optional in prototype.
- Following program illustrates the value of function parameters :
void sqr_it(int *i); //prototype of function sqr_it
num = 10;
sqr_it(num); //type mismatch
void sqr_it(int *i)
*i = *i * *i;
- Since sqr_it() has pointer to integer as its parameter, the program throws an error when we pass an integer to it.
What is function prototype in C++?
- A function prototype is a declaration of a function that omits the function body. It specifies the function's name, argument types and return type.
- Example :