Prototypes

A function prototype, consists of a return data type and function signature, but has no code block and is terminated by a semi-colon ;

 

Syntax:

return_datatype function_name (parameter, list) ;

 

This informs the compiler of what to expect in terms of memory resources for the function, by letting main() know what data type it returns along with the argument data types the function expects.

 

The format of the prototype must match the actual fully coded function (in terms of the return data type and function signature), or there will be compiler errors.

 

Prototypes allow a program to be developed without having to completely work out all the code for each function in advance. This approach allows the logical flow of the main() section of the program to be developed, and then concentrate upon the functions code body.

 

Remembering, all functions and variables must be declared before they can be used.

 

The full code of the function is then defined after main()

Compile & Run:

Before swap: 17, 42
After swap: 42, 17

 

 

This methodology is called function prototyping, and provides a neat way to structure your programs with as many functions as required following main().

 

This also gives the code good readability, as one can easily dip in and out of main() when reading the code, to view the required function, understand what its doing and what its returning (if anything) back into main().

Leave a Reply