C Function Declaration and Definition
1. What is Function Declaration?
-
A function declaration (also called prototype) tells the compiler about a function’s name, return type, and parameters before its actual definition.
-
It does not contain the function body.
-
Helps the compiler check for correct function usage.
Syntax:
Example:
2. What is Function Definition?
-
A function definition contains the actual body of the function — where the task is performed.
-
It tells the compiler what the function does.
Syntax:
Example:
3. Calling a Function
-
You can call a function after declaration or definition.
-
Example:
Output:
Function declaration allows you to define the function later (after
main).
4. Function Without Declaration
-
If the function is defined before
main, a declaration is optional.
5. Function Declaration for Different Return Types
-
The compiler uses declaration to check type correctness when calling the function.
6. Key Points About Declaration and Definition
-
Declaration (Prototype): informs the compiler about the function.
-
Definition: provides the actual function body.
-
A function must be declared before it is called, either via prototype or definition.
-
Declarations allow calling a function before its definition, which is common in large programs.
-
Function declaration improves code readability and compiler checking.
