A **Function Declaration** (also known as a **Function Prototype**) tells the compiler about a function's name, return type, and parameters before the function is actually defined. This is essential for maintaining organized code, especially when functions call each other or are defined after the main() function.
To use functions professionally, you must distinguish between these two phases:
In C, if you define a function *after* the main() function without a declaration at the top, the compiler will assume the function returns an int and might not check the parameters correctly. This leads to "implicit declaration" errors.
A declaration consists of three main parts, which must perfectly match the definition that follows later:
| Component | Purpose |
|---|---|
| Return Type | What kind of value the function sends back (int, void, etc.). |
| Function Name | The identifier used to invoke the code. |
| Parameter List | The types of data the function expects (e.g., int, float). |
In large professional projects, declarations are usually placed in Header Files (.h files), while definitions are kept in Source Files (.c files). This is how libraries like stdio.h work. You see the declaration (prototype) when you include the header, but the actual code (definition) is hidden in the pre-compiled library.
;), while definitions *must not* have a semicolon before the curly braces.int add(int a, int b); but the definition says float add(int a, int b) {...}, the compiler will throw a "conflicting types" error.void sum(int, int); is perfectly valid, though void sum(int a, int b); is better for readability.#include statements. This makes your code modular and ensures that functions can call each other regardless of the order in which they are defined.