Loading, please wait...

Function Prototype

One of the most important features of C is the function prototype. A function prototype tells the compiler the type of data returned by the function, the number of parameters the function expects to receive, the types of the parameters, and the order in which these parameters are expected. The compiler uses function prototypes to validate function calls. Previous versions of C did not perform this kind of checking, so it was possible to call functions improperly without the compiler detecting the errors. Such calls could result in fatal execution-time errors or nonfatal errors that caused subtle, difficult-to-detect logic errors. Function prototypes correct this deficiency.

 

Include function prototypes for all functions to take advantage of C’s type-checking capabilities. Use #include preprocessor directives to obtain function prototypes for the standard library functions from the headers for the appropriate libraries, or to obtain headers containing function prototypes for functions developed by you and /or your group members.

 

The function prototype for the maximum in  is

int maximum( int x, int y, int z ); /* function prototype */

 

This function prototype states that maximum takes three arguments of type int and returns a result of type int. Notice that the function prototype is the same as the first line of the function definition of maximum.

For example, if the function prototype had been written

void maximum( int x, int y, int z );

the compiler would generate an error because the void return type in the function prototype would differ from the int return type in the function header.

The promotion rules automatically apply to expressions containing values of two or more data types (also referred to as mixed-type expressions). The type of each value in a mixed-type expression is automatically promoted to the “highest” type in the expression (actually a temporary version of each value is created and used for the expression the original values remain unchanged). The table lists the data types in order from highest type to lowest type with each type’s printf and scanf conversion specifications.

 

Data type

Printf conversion specification

Scanf conversion specification

long double

%Lf

%Lf

double

%f

%lf

float

%f

%f

unsigned long int

%lu

%lu

long int

%ld

%ld

unsigned int

%u

%u

int

%d

%d

unsigned short

%hu

%hu

short

%hd

%hd

char

%c

%c

 

Converting from a higher data type in the promotion hierarchy to a lower type can change the data value. Many compilers issue warnings in such cases. If there is no function prototype for a function, the compiler forms its own function prototype using the first occurrence of the function either the function definition or a call to the function. This typically leads to warnings or errors, depending on the compiler. Always include function prototypes for the functions you define or use in your program to help prevent compilation errors and warnings.

 

A function prototype placed outside any function definition applies to all calls to the function appearing after the function prototype in the file. A function prototype placed in a function applies only to calls made in that function.