Next: Exercises Up: Functions Previous: Functions and Arrays

Function Prototyping

Before you use a function C must have knowledge about the type it returns and the parameter types the function expects.

The ANSI standard of C introduced a new (better) way of doing this than previous versions of C. (Note: All new versions of C now adhere to the ANSI standard.)

The importance of prototyping is twofold.

How this is done depends on the scope of the function (See Chapter ). Basically if a functions has been defined before it is used (called) then you are ok to merely use the function.

If NOT then you must declare the function. The declaration simply states the type the function returns and the type of parameters used by the function.

It is usual (and therefore good) practice to prototype all functions at the start of the program, although this is not strictly necessary.

To declare a function prototype simply state the type the function returns, the function name and in brackets list the type of parameters in the order they appear in the function definition.

e.g.


   int strlen(char []);

This states that a function called strlen returns an integer value and accepts a single string as a parameter.

NOTE: Functions can be prototyped and variables defined on the same line of code. This used to be more popular in pre-ANSI C days since functions are usually prototyped separately at the start of the program. This is still perfectly legal though: order they appear in the function definition.

e.g.


   int length, strlen(char []);

Here length is a variable, strlen the function as before.


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994