What are function prototypes?

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
int main()
{
   int num;
   num = 10;
   sqr_it(num); //type mismatch
   return 0;
}
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 :
int add(int,int)
What is function overloading??
What is function overloading? - Function overloading is the process of using the same name for two or more functions...
What are default arguments in functions? - C++
What are default arguments in functions? - C++ allows a function to assign a parameter a default value when no argument corresponding to that parameter is specified in a call to that function....
What is static function? - C++
What is static function? - Static member functions are used to maintain a single copy of a class member function across various objects of the class....
Post your comment