The C Language

a function prototype

Before you call a contractor, it helps to know up front what they need from you — what materials to supply and what they will deliver — even if you have not seen them do the actual work yet. A function prototype is that advance notice for a function: it tells the compiler the function's name, the types of its parameters, and its return type, without the body. With that, the compiler can check every call site even before it has met the real definition.

A prototype looks like a definition with the body replaced by a semicolon: 'int add(int a, int b);'. It announces the function's shape. When the compiler then sees a call like add(2, 3), it can verify you passed the right number and types of arguments and that you use the result correctly. Prototypes typically live in header files so that many source files can call a function whose definition sits in just one of them. They also enable forward use: a function can be called earlier in a file than where it is defined, as long as its prototype appeared first.

Why this matters: without a visible prototype, older C would assume undeclared rules about a function's arguments and return type, which silently masks mistakes — for instance passing a double where an int was wanted. Modern C requires you to declare functions before use, and good practice is one prototype in a header included everywhere the function is called. The prototype is a declaration, not a definition: it has no body and allocates no code; the definition supplied elsewhere must match it exactly.

/* prototype, often in a header */ int add(int a, int b); /* a caller, checked against the prototype */ int main(void) { return add(2, 3); } /* the matching definition, elsewhere */ int add(int a, int b) { return a + b; }

The prototype lets main's call to add be type-checked before the compiler ever sees add's body.

A prototype is a declaration, not a definition — it has no body. Its parameter and return types must match the real definition exactly; a mismatch is undefined behavior, not a guaranteed error. Use int f(void), not int f(), to declare 'takes no arguments'.

Also called
function declarationforward declaration函式宣告前向宣告