What is a Function?
A function is a block of code that performs a specific task and can be used again and again.
Simple definition (exam-ready):
A function is a self-contained block of code that performs a particular task.
1. Why Do We Need Functions?
Without functions, everything is written inside main(), which becomes:
- Very long
- Hard to read
- Hard to debug
Functions help to:
- Divide a program into small parts
- Improve code readability
- Enable reusability
- Reduce errors
2. Real-Life Example
Think of a calculator:
- Addition is one task
- Subtraction is another task
Each task = function
3. Types of Functions in C
- Library functions
Examples:
printf()scanf()strlen()
- User-defined functions
Functions written by the programmer. we will learn later in next topic
