In C++, you need to understand the difference between function declarations and function definitions.
A declaration says "a function called somename exists and this is its interface".
extern int somename(int x, int y);
A definition says "a function called somename exists and this is how it is implemented".
int somename(int x, int y)
{
return x + y + 1;
}
A header is typically used to specify the interface provided by one or more classes, or the interface provided by one or more functions. It may provide other information that users of the code need to know. A header is intended for use by more than one source file; there isn't a lot of point in having a header that is intended for use by a single source file (though there are some cases where it makes sense).
So, normally, a header provides declarations of functions.
To complicate matters, functions can be defined inline. When a function is defined inline, then you place its implementation into a file (often a header file) and the compiler might optimize the use of the function to avoid the overhead of a function call. So you might create a header containing:
inline somename(int x, int y) { return x + y + 1; }
Without the inline
keyword, this would lead to a violation of the ODR - One Definition Rule. The ODR says that a function or globally visible object may only be defined once in the program. If the keyword inline
was omitted in the header, but the header was used in more than file, then each file would define somename()
and the files could not all be linked together because there would be multiple definitions of somename()
.