Question : Why do we need header files? What do I put in header files?
Answer : Header files allow you to share common information among various source files.
For a function, we define the function declaration as the return type, the name of the function, and the
list of arguments. The function definition is the function declaration followed by the function body. For
// function declaration
double compute_root( double a, double b, double c );
// function definition
double compute_root( double a, double b, double c )
double delta = b * b - 4 * a * c;
if( delta < 0 )
cout << "Error!" << endl;
return ( - b + sqrt( delta ) ) / ( 2 * a );
Obviously, the declaration and the definition have to match for the same function!
Suppose you create some functions called A, B, and C and you put them in the same source file called
myfuncs.C. Let's suppose that the function A needs to call another small function called A1. We can put
A1 in myfuncs.C also.
In C++ (and C), the functions A, B, C, and A1 are only known only inside the file myfuncs.C. Inside that
file, any function (A, B, C, or A1) can call any other function. If you write a main function and put it in a
file main.C, it will not know about the functions A, B, C, or A1. If you try to call A (or B, or C, or A1)
from main, you will get a compile-time error, because the compiler does not know about those functions
when it reads the main.C file.
To fix this, we have to somehow have main.C know about the functions it uses. The easiest way to do this
is to put the function definitions for the function used by main inside the main.C file. So we can do this for
every file that uses the functions A, B, or C. This is one solution, but it is incomplete. What happens if we
add one parameter to the arguments of the function A? Then we have to go and modify all the files that
use the function A to know about the new parameter! This is cumbersome and prone to error.
We can just put the function declarations in a header file named myfuncs.h (so we know it contains
declarations for functions in myfuncs.C) and #include myfuncs.h in each source file that uses those
functions. This way, we just need to modify the header file myfuncs.h and the source file myfuncs.C
whenever we change the function arguments.
So header files (.h) help us share common information among source files (.C).
If functions A, B, and C, in file1.C are called from file2.C, create a header file named file1.h, put
the declarations of A, B, and C in file1.h, and #include "file1.h" in file2.C.
for structures (struct, union, class):
If the structure is used by functions in file1.C and file2.C, create a header file named common.h,
put the definition of the structure in common.h, and #include "common.h" in both file1.C and
for constants (const int, const double, ...):
Similar to structures.
Obviously, the rules above can be extended to more than two files.
Keep in mind that these rules are not part of the language, the compiler does not enforce them, but they
are standard programming practice.