Motivation
In most modern computer programming languages, programmers can break up programs into smaller components (such as classes and subroutines) and distribute those components among many translation units (typically in the form of source files), which the system can compile separately. Once a subroutine needs to be used somewhere other than in the translation unit where it is defined, a way to refer to it must exist. For example, a function defined in this way in one source file:
int add(int a, int b) { return a + b; }may be declared (with a function prototype) and then referred to in a second source file as follows:
int add(int, int); int triple(int x) { return add(x, add(x, x)); }One drawback of this method is that the prototype must be present in all files that use the function. Another drawback is that if the return type or arguments of the function are changed, these prototypes will have to be updated. This process can be automated with the C preprocessor (i.e., getting any prototype results in getting the latest one). For example, the following code declares the function in a separate file (the parameter names are not used by the compiler, but they can be useful to programmers):
- File "add.h"
This file uses include guards to avoid multiple definitions of the function. The following code demonstrates how header files are used:
#include "add.h" int triple(int x) { return add(x, add(x,x)); }Now, every time the code is compiled, the latest function prototypes in add.h will be included in the files using them, avoiding potentially disastrous errors.
Read more about this topic: Header File
Famous quotes containing the word motivation:
“Self-determination has to mean that the leader is your individual gut, and heart, and mind or were talking about power, again, and its rather well-known impurities. Who is really going to care whether you live or die and who is going to know the most intimate motivation for your laughter and your tears is the only person to be trusted to speak for you and to decide what you will or will not do.”
—June Jordan (b. 1939)