In this article, you will gain deeper insight about function prototype in C programming.

function prototype in c

Function prototype is the important feature of C programming which was borrowed from C++. Early versions of C programming did not use function prototype.

Function prototype in C is a function declaration that provides information to the compiler about the return type of the function and the number, types, and order of the parameters the called function expect to receive.

Function prototype in C programming: Importance


Function prototype in C is used by the compiler to ensure whether the function call matches the return type and the correct number of arguments or parameters with its data type of the called function.

In the absence of the function prototype, a coder might call function improperly without the compiler detecting errors that may lead to fatal execution-time errors that are difficult to detect.

Syntax of function prototype in C programming

return_type function_name( type argument1, type argument2, ...);
Good Programming Practice
We should explicitly include function prototype for every user defined function to increase type-checking capability of the compiler and use #include preprocessor directive for function prototypes of standard library functions.

 

Example of function prototype


Let’s consider following function definition:

int area( int length, int breadth )   //function definition
{
   ....     //function body
}

Now, the corresponding prototype declaration of the above function is:

int area( int length, int breadth );    //function prototype

It states that function area takes two arguments of type int and returns area of type int.

Difference between function prototype and function definition in C


The only difference between the function definition and its function prototype is the addition semicolon (;) at the end of prototype declaration.

But, the parameter identifier could be different in function prototype and function definition because the scope of parameter identifier in a function prototype is limited within the prototype declaration.

Actually, the compiler ignores the name of the parameter list in the function prototype. Having said that, it is good programming practice to include parameter names which increase program clarity.

[adsense1]

Please carefully observe the following prototype declaration:

int area(int length, int breadth );
int area(int x, int y );
int area(int , int );

All of the above function prototypes are same.

Please note that a function call that does not match prototype declaration is a compilation error.

Function prototype in C: Scope and Conversion


The scope of the function prototype in C is determined by its position in the program. Generally, the function prototype is placed after the header file in the program. The scope of the function prototype is considered within the same block as the function call.

Another interesting feature of function prototype is argument conversion.

For example, the standard math library function sqrt has a double type parameter in the function prototype. However, it can be called with an integer argument and works perfectly.

x = sqrt(16);

The output of this statement is:

x = 4.000

As we talked earlier about conversion feature, the compiler converts a copy of integer value 16 to the double 16.0 before passing to sqrt.

Note: Please follow C programming usual arithmetic conversion rule to avoid truncation error.