Functions in C Programming
A function in C is a block of code that performs a specific task.
It helps in code reusability, modularity, and makes programs easy to understand.
Why use functions?
To avoid repeating the same code.
To make code cleaner and organized.
To divide large programs into smaller parts.Types of Functions
There are mainly two types:
- Library Functions:
Already available in C libraries.
Examples:
printf()
scanf()
sqrt()
Examples:
printf()2. User-Defined Functions
Functions created by the programmer.
Function Syntax
Function Example (Simple Addition)
Output:
Result = 15
Result = 15
Parts of a Function
- Function Declaration (Prototype)
Tells the compiler about the function name, return type, and parameters.
int add(int, int); 2. Function Definition
Actual body of the function.
int add(int a, int b) {
return a + b;
}
}
int add(int a, int b) {
return a + b; }
}
0 Comments