6.4. Function Prototypes and Argument Coercion

A function prototype (also called a function declaration) tells the compiler the name of a function, the type of data it returns, the number of parameters it expects to receive, the types of those parameters and the order in which the parameters of those types are expected.


Image Software Engineering Observation 6.3

Function prototypes are required unless the function is defined before it is used. Use #include preprocessing directives to obtain function prototypes for the C++ Standard Library functions from the headers of the appropriate libraries (e.g., the prototype for sqrt is in header <cmath>; a partial list of C++ Standard Library headers appears in Section 6.5). Also use #include to obtain headers containing function prototypes written by you or other programmers.



Image Common Programming Error 6.3

If a function is defined before it’s invoked, then its definition also serves as the function’s prototype, so a separate prototype is unnecessary. If a function is invoked before it’s defined, and that function does not have a function prototype, a compilation error occurs.



Image Software Engineering Observation 6.4

Always provide function prototypes, even though it’s possible to omit them when functions are defined before they’re used. Providing the prototypes avoids tying the code to the order in which functions are defined (which can easily change as a program evolves).


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.212.211