Wednesday, December 16, 2009

5.4 FUNCTION PROTOTYPES

In the programs that we have examined earlier in this chapter,the programmer defined function has always preceded the main.Thus,when these programs are compiled,the programmer defined function will have been defined before the first function access.However, many programmers prefer a "top-down" approach,in which the main appears ahead of the programmer defined function definition.In such situations the function access (within main) will precede the function definition.This can be confusing to the compiler,unless the compiler is first alerted to the fact that the function being accessed will be defined later in the program.A function prototype is used for this purpose.

Function prototypes are usually written at the beginning of a program,ahead of any programmer defined functions (including main).The general form of a function prototype is

data-type name (type 1 arg 1,type 2 arg 2,.........,type n arg n);

where data-type represents the data type of the item that is returned  by the function,name represents the function name, and  type 1, type 2,............., type n represent the data types of the arguments arg 1,         arg 2,...., arg n.Notice that a function prototype resembles the first line of a function definition (though a function prototype end with semicolon).

Function prototypes are not mandatory in C.They are desirable,however, because they further facilitate error checking between the calls to function and the corresponding function definition.

No comments:

Post a Comment