A standard C header file contains the declarations or prototypes of functions of a particular category. A function ‘prototype usually specifies the type of value returned by that function, the function name and a list specifying parameter types as
ret_type func_name ( type1 , type2, … ) ;
where func_name is the name of the function, ret_type is the type of value returned by it and typel, type2, … are the types of first, second, … function parameters. Note that the parameter type list is comma separated and is enclosed within a pair of parentheses. It specifies the number of arguments required in function calls and the type of each argument. When a function is called, an argument of appropriate type must be specified for each of the parameter types specified in the function prototype.
We can either omit the parameter type list or use thevoid keyword in its place to indicate that the function does not have any parameters, i. e., it does not require any arguments. In addition, the void keyword may be used as the function return type to indicate that the function does not return any value. However, note that if the return type is omitted, the function is assumed to return a value of typeint.
A function prototype may also contain the parameter names as shown below:
ret_type func_name (type1 param1, type2 param2, … );
where param1 is the name of first parameter, param2 is the name of the second parameter and so on. The inclusion of parameter names in function prototypes clarifies the order in which arguments are to be specified in a function call.
Example of Function Prototypes
For example, the prototypes of thesqrt andpow standard library functions are given below.
double sqrt(double);
double pow(double, double);
Thesqrt function returns the square root of the argument expression specified in its call. It accepts one argument of typedouble and returns a value of typedouble.On the other hand, thepow function which calculates xY accepts two arguments of typedouble and returns a value of typedouble. This prototype can also be written using parameter names as
double pow(double x, double y);
Now consider the prototypes for thegetmaxx andsetcolor functions given in thegraphics.h header file of Turbo C/C++.
int getmaxx(void);
void setcolor(int color);
Observe that thegetmaxx function does not accept any argument and thesetcolor function does not return any value.
Illustrates declaration of function prototype. (Function definition is given below the main function).
#include<stdio.h>
int Cube (int); // Prototype of function Cube()
int Square(int); //prototype of function Square()
main()
{
int A;
clrscr();
printf("Number\t Square\t Cube\n" );
for( A= 1;A <=4 ;A++)
printf("%d\t %d\t %d\n", A, Square(A), Cube (A) );
return 0;
}
int Cube (int x) // Definition of function Cube()
{return x*x*x;}
int Square (int y) // Definition of function Square()
{return y*y;}
In the above program, two functions are defined: One returns the square of its argument and the other returns cube of its argument. They are called in the printf()function in main to return the respective values for numbers 1 to 4 which are printed in the output.
Program is another example in which the function prototype has been declared before main and function definition is given after main().