Types of Functions in C
Types of Functions in C
Introduction
Functions are an important part of C programming. They help divide a program into smaller, manageable parts.
In C, functions are classified based on:
- Whether they take arguments
- Whether they return values
👉 Understanding types of functions helps you write better and more efficient programs.
Categories of Functions
Functions in C are mainly divided into:
- Library Functions
- User-defined Functions
1. Library Functions
These are predefined functions provided by C.
👉 Examples:
-
printf() -
scanf() -
sqrt() -
strlen()
👉 You just use them — no need to define.
2. User-defined Functions
Functions created by the programmer.
👉 These are further divided into 4 types:
Types Based on Arguments & Return Value
| Type | Arguments | Return Value |
|---|---|---|
| Type 1 | No | No |
| Type 2 | Yes | No |
| Type 3 | No | Yes |
| Type 4 | Yes | Yes |
Type 1: No Arguments, No Return Value
Example
#include <stdio.h>
void display() {
printf("Hello from function");
}
int main() {
display();
return 0;
}
Output
Hello from function
Type 2: With Arguments, No Return Value
Example
#include <stdio.h>
void add(int a, int b) {
printf("Sum = %d", a + b);
}
int main() {
add(5, 3);
return 0;
}
Output
Sum = 8
Type 3: No Arguments, With Return Value
Example
#include <stdio.h>
int getNumber() {
return 10;
}
int main() {
int num = getNumber();
printf("Number = %d", num);
return 0;
}
Output
Number = 10
Type 4: With Arguments, With Return Value
Example
#include <stdio.h>
int multiply(int a, int b) {
return a * b;
}
int main() {
int result = multiply(4, 5);
printf("Result = %d", result);
return 0;
}
Output
Result = 20
Flow of Function Types
- Function call → Pass arguments (if any)
- Function executes
- Returns value (if any)
Advantages of Using Functions
- ✔ Code reusability
- ✔ Modularity
- ✔ Easy debugging
- ✔ Better readability
Common Mistakes
- ❌ Mismatch in arguments
- ❌ Wrong return type
- ❌ Missing return statement
- ❌ Incorrect function call
Pro Tips
- ✔ Choose correct function type
- ✔ Use meaningful names
- ✔ Keep functions small
- ✔ Avoid unnecessary arguments
Conclusion
Understanding types of functions helps you design better programs. It allows you to choose the right approach based on your needs.
Mastering functions is a key step toward advanced programming.
👉 This article is part of Dharani Tech Edu Hub — where learning programming is made simple and practical.
Comments
Post a Comment