|
|
|
|
|
|
|
Exactly what are in these header files? |
|
|
|
|
|
|
|
|
It turns out that there is nothing magical about header files. Their contents are nothing more than a series of C++ declarations. There are declarations of named constants such as INT_MAX and INT_MIN, and there are declarations of stream variables like cin and cout. But most of the items in a header file are function prototypes. |
|
|
|
|
|
|
|
|
Suppose that your program needs to use the library function sqrt in a statement like this: |
|
|
|
|
|
|
|
|
Every identifier must be declared before it can be used. If you forget to #include the header file math.h, the compiler gives you an UNDECLARED IDENTIFIER error message. The file math.h contains function prototypes for sqrt and all of the other math-oriented library functions. With this header file included in your program, the compiler not only knows that the identifier sqrt is the name of a function but it also can verify that your function call is correct with respect to the number of parameters and their data types. |
|
|
|
|
|
|
|
|
Header files save you the trouble of specifying all of the library function prototypes yourself at the beginning of your program. With just one linethe #include directiveyou cause the preprocessor to go out and find the header file and insert the prototypes into your program. In later chapters, we see how to create our own header files that contain declarations specific to our programs. |
|
|
|
|
|
|
|
|
When a function is executed, it uses the actual parameters given to it in the function call. How is this done? The answer to this question depends on the nature of the formal parameters. C++ supports two kinds of formal parameters: value parameters and reference parameters. With a value parameter, which is declared without an ampersand (&) at the end of the data type name, the function receives a copy of the actual parameter's value. With a reference parameter, which is declared by adding an ampersand to the data type name, the function receives the location (memory address) of the actual parameter. Before we examine in detail the difference between these two kinds of parameters, let's look at an example of a function heading with a mixture of reference and value parameter declarations. |
|
|
|
|
|
|
|
|
void Example( int& paraml, // A reference parameter
int param2, // A value parameter
float param3 ) // Another value parameter |
|
|
|
|
|