C Function Declaration and Definition

C Function Declaration and Definition
In C language, functions help in breaking large programs into smaller, reusable blocks.
To use functions correctly, you must understand function declaration and function definition.
What is a Function in C?
A function is a block of code that performs a specific task and can be called multiple times.
Function Declaration (Function Prototype)
What is Function Declaration?
A function declaration tells the compiler:
Function name
Return type
Parameters
It does not contain function body.
Syntax
Example
- Informs compiler that
add()exists somewhere
Function Definition
What is Function Definition?
A function definition contains:
Function header
Function body (actual code)
Syntax
Example
Complete Program Example
- Declaration before
main() - Definition after
main()
Why Function Declaration Is Needed?
- Enables calling function before its definition
- Required for multi-file programs
- Helps compiler in type checking
Without declaration → compiler error
Declaration vs Definition (Important)
| Feature | Declaration | Definition |
|---|---|---|
| Tells about function | Yes | Yes |
| Contains body | No | Yes |
| Memory allocated | No | Yes |
| Can be repeated | Yes | No |
Function Call Flow
Multiple Declarations Allowed
- Multiple definitions are NOT allowed
Function Declaration in Header Files
math_utils.h
math_utils.c
main.c
- Used in large projects
Common Mistakes
- Missing function declaration
- Mismatch between declaration & definition
- Wrong return type
- Wrong parameter order
Declaration & definition must match exactly
Interview Questions (Must Prepare)
What is function declaration?
What is function definition?
Why function prototype is required?
Difference between declaration & definition
Can we define function before
main()?
Real-Life Use Cases
Modular programming
Library development
Embedded systems
Operating systems
Large-scale applications
Summary
- Function declaration informs compiler
- Function definition provides implementation
- Both are essential in C
- Very important for placements & interviews
