Lesson 12 / 36
Function Prototypes
Tell the compiler about a function before you define it.
Why prototypes?
C reads top to bottom. If main calls a function defined later in the file, the compiler needs a prototype — the function's signature ending in ; — declared earlier.
Declare, then define
The prototype's parameter names are optional and only for documentation.
#include <stdio.h>
int square(int n); // prototype
int main(void) {
printf("%d\n", square(5));
return 0;
}
int square(int n) { // definition
return n * n;
}
Output:
25
Header files hold prototypes
<stdio.h> and friends are just files full of prototypes for functions like printf, so the compiler trusts your calls before linking.