Function Declarations & Header Files
Prerequisites: Functions
A function must be declared (its signature known to the compiler) before it's called — in a single file, defining it earlier in the file satisfies this. A prototype declares just the signature, with the actual body coming later (or in another file):
int add(int a, int b); // prototype
int main(void) {
printf("%d\n", add(2, 3)); // fine — already declared above
return 0;
}
int add(int a, int b) { // definition
return a + b;
}
This is exactly what a header file (.h) is for in a multi-file
program — a shared collection of prototypes other files #include
so they can call functions defined elsewhere (full treatment in
Multi-File Programs, Level 2).
Common mistakes
- Calling a function before either its prototype or its full definition has appeared — "implicit declaration" errors (or, in older/looser compiler modes, silently wrong behavior).