C Function Declaration and Definition
The C compiler reads your file from top to bottom, once. When it meets a function call, it must already know what that function looks like: its name, return type and parameter types. So far we satisfied this by writing every function above main. As programs grow, that becomes awkward — and it is impossible when two functions call each other. The solution is to separate a function's declaration from its definition.
Declaration vs. definition
| Part | What it contains | Example |
|---|---|---|
| Declaration (prototype) | return type, name, parameter types, then ; |
int add(int a, int b); |
| Definition | the declaration plus the body | int add(int a, int b) { return a + b; } |
A declaration is a promise: "a function like this exists somewhere." The definition keeps the promise. You can declare a function many times, but define it only once.
Putting functions after main
Declare the functions at the top, call them from main, and define them below. Many programmers like this layout because main — the overview of the program — comes first:
#include <stdio.h>
// Declarations (prototypes)
double rectangle_area(double width, double height);
void print_line(int length);
int main(void) {
print_line(20);
printf("Area: %.2f\n", rectangle_area(3.5, 2.0));
print_line(20);
return 0;
}
// Definitions
double rectangle_area(double width, double height) {
return width * height;
}
void print_line(int length) {
for (int i = 0; i < length; i++) {
putchar('-');
}
putchar('\n');
}--------------------
Area: 7.00
--------------------
Parameter names are optional in a declaration — double rectangle_area(double, double); is valid — but including them documents what each parameter means.
What happens without a declaration?
If you call a function the compiler has not seen yet, modern GCC reports an error (older C standards assumed it returned int, which caused subtle bugs):
#include <stdio.h>
int main(void) {
printf("%d\n", triple(5)); // error: implicit declaration of function 'triple'
return 0;
}
int triple(int x) { return 3 * x; }Adding int triple(int x); above main fixes it.
Functions that call each other
When function A calls B and B calls A (mutual recursion), no ordering of the definitions works on its own. A declaration solves it:
#include <stdio.h>
#include <stdbool.h>
bool is_odd(unsigned n); // declared first so is_even can call it
bool is_even(unsigned n) {
if (n == 0) return true;
return is_odd(n - 1);
}
bool is_odd(unsigned n) {
if (n == 0) return false;
return is_even(n - 1);
}
int main(void) {
for (unsigned n = 0; n <= 5; n++) {
printf("%u is %s\n", n, is_even(n) ? "even" : "odd");
}
return 0;
}0 is even
1 is odd
2 is even
3 is odd
4 is even
5 is odd
Declarations in header files
In larger projects, functions are spread over several .c files. Their declarations go into a header file (.h) that every file using them includes. That is exactly what #include <stdio.h> does: it pulls in the declarations of printf and friends, while their definitions live in the C library.
// geometry.h — declarations only
#ifndef GEOMETRY_H
#define GEOMETRY_H
double circle_area(double r);
double square_area(double side);
#endif// geometry.c — definitions
#include "geometry.h"
double circle_area(double r) { return 3.14159265 * r * r; }
double square_area(double side) { return side * side; }// main.c — uses them
#include <stdio.h>
#include "geometry.h"
int main(void) {
printf("%.2f\n", circle_area(2.0));
return 0;
}gcc main.c geometry.c -o appAngle brackets <...> search the system include directories; quotes "..." search your project first. The #ifndef / #define / #endif lines are an include guard that prevents the header from being processed twice (see C Preprocessor & Macros).
() vs. (void) in declarations
In C17, an empty parameter list in a declaration means "parameters not specified", so the compiler cannot check calls. (void) means "takes no parameters" and lets the compiler catch mistakes:
#include <stdio.h>
int answer(void); // calling answer(1, 2) would be a compile error
int main(void) {
printf("The answer is %d\n", answer());
return 0;
}
int answer(void) {
return 42;
}The answer is 42
Tip: Always write prototypes with full parameter types (and
voidfor none). They let the compiler check every call — the number of arguments, their types, and the use of the return value.
Test yourself
Which line is a function declaration (prototype)?
- int max(int a, int b) { return a > b ? a : b; }
- int max(int a, int b);
- max(3, 4);
- return max;
Why might you need a declaration when two functions call each other?
- To make them faster
- Whichever is defined first would call a function the compiler has not seen yet
- C forbids mutual calls
- Declarations are always required twice
Where are function declarations usually placed in multi-file projects?
- In .h header files
- Inside main
- At the end of each .c file
- In comments