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.
1️⃣ What is a Function in C?
A function is a block of code that performs a specific task and can be called multiple times.
2️⃣ 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
3️⃣ Function Definition
🔹 What is Function Definition?
A function definition contains:
-
Function header
-
Function body (actual code)
Syntax
Example
4️⃣ Complete Program Example ⭐
✔ Declaration before main()
✔ Definition after main()
5️⃣ 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
6️⃣ 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 |
7️⃣ Function Call Flow
8️⃣ Multiple Declarations Allowed
❌ Multiple definitions are NOT allowed
9️⃣ 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
