Function Declaration

Like variables, all the functions of a C program must be declared before they can be called. A function declaration consists of four parts.

  1. Function type(return type).
  2. Function name.
  3. Parameter list.
  4. Terminating semicolon.

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 */

Note :

  1. The list of parameters must be separated by commas.
  2. The parameter names do not need to be the same in the prototype declaration and function definition.
  3. The types match the type of parameters in the definition of the function in numerical order.
  4. The names of the parameters in the declaration are optional.
  5. If the function has no official parameters, the list is written as (void).
  6. Return type is optional when the function returns in-type data.
  7. The return type must be void if no value is returned.
  8. When the declared types do not match with the types in the function definition, compiler will produce an error.

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 :

void display(void);

A prototype can be announced in two places in a program.

  1. Above all functions (including main).
  2. Inside the definition of a function.

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.

Prototypes : Yes or No

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.