Answers

Question and Answer:

  Home  C++ Type Checking

⟩ Do you know 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.

 183 views

More Questions for you: