Like variables, all functions in a C program must be declared, before they are invoked. A function declaration (also known as function prototype) consists of four parts.
- Function type (return type)
- Function name
- Parameter list
- Terminating semicolon
They are coded in the following formate : Function-type function-name (parameter list);
This is very similar to the function header line except the terminating semicolon. For example, mul function defined in the previous section will be declared as : int mul (int m, int n); /* Function prototype */
Points to note – Function Declaration
- The parameter list must be separated by commas.
- The parameter names do not need to be the same in the prototype declaration and the function definition.
- The types must watch the types of parameters in the function definition, in number and order.
- Use of parameter names in the declaration is optional.
- If the function has no formal parameters, the list is written as (void).
- The return type is optional, when the function returns int type data.
- 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 mul function are
int mul (int, int);
mul (int a, int b);
mul (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 declaration may be placed in two places in a program.
- Above all the functions (including main).
- Inside a function definition.
When we place the declaration above all the functions (in the global declaration section), the prototype is referred to as a global prototype. Such declarations are available for all the functions in the program.
When we place it in a function definition (in the local declaration section), the prototype is called a local prototype. Such declarations are primarily used by the functions containing them.
The place of declaration of a function defines a region in a program in which the function may be used by other functions. This region is known as the scope of the function. (Scope is discussed later in this chapter.) It is a good programming style to declare prototypes in the global declaration section before main. It adds flexibility, provides an excellent quick reference to the functions used in the program, and enhances documentation.
Prototypes: Yes or No
Prototype declarations are not essential. If a function has not been declared before it is used, C will assume that its details available at the time of linking. Since the prototype is not available, C will assume that the return type is an integer and that the types of parameters match the formal definitions. If these assumptions are wrong, the linker will fail and we will have to change the program. The moral is that we must always include prototype declarations, preferably in global declaration section.
Parameters Everywhere!
Parameters (also known as arguments) are used in three places:
- in declaration (prototypes),
- in function call, and
- in function definition.
The parameters used in prototypes and function definitions are called formal parameters and those used in function calls are called actual parameters. Actual parameters used in a calling statement may be simple constants, variables or expressions.
The formal and actual parameters must match exactly in type, order and number. Their names, however, do not need to match.
Category of Function Declaration
A function, depending on whether arguments are present or not and whether a value is returned or not, may belong to one of the following categories:
Category 1: Functions with no arguments and no return values.
Category 2: Functions with arguments and no return values.
Category 3: Functions with arguments and one return value.
Category 4: Functions with no arguments but return a value.
Category 5: Functions that return multiple values.
In the sections to follow, we shall discuss these categories with examples. Note that, from now on, we shall use the term arguments (rather than parameters) more frequently.
Read More Topics |
Error handling file I/O |
Disk file I/O streams |
Pointer to pointer in C++ |