Like variables, all the functions of a C program must be declared before they can be called. A function declaration consists of four parts.
They are coded in the following format :
function-type function-name(parameter list);
This is very similar to the function header line except for the terminating semicolon. For example, the sum() function defined in the previous tutorials will be declared as :
int sum(int a, int b); /* Function prototype */
Equally acceptable forms of declaration of sum function are :
int sum(int, int); sum(int a, int b); sum(int, int);
When a function does not take any parameters and does not return any value, its prototype is written as :
A prototype can be announced in two places in a program.
When we place the declaration above all actions (in the global declaration section), the prototype is referred to as the global prototype. Such announcements are available for all functions in the program.
When we put it in a function definition (in the local declaration section), the prototype is called a local prototype. Such declarations are mainly used by the functions they contain.
The function declaration space defines an area of a program where the function can be used by other functions. This area is known as the scope of the function. Announcing prototypes in the global announcement section before the original is a good programming style. It adds flexibility, provides a great quick reference to the functions used in the program, and enhances documentation.
Prototype announcement is not necessary. If a function is not declared before use, C will assume that its link is available at the time of the link. Since the prototype is not available, C will assume that the type of return is an integer and the types of parameters match the formal definitions. If these assumptions are incorrect, the linker will fail and we need to change the program. The ethic is that we must always include prototype announcements at the forefront of the global announcement section.