Functions in C
1️⃣ What is a Function?
A function in C is a block of code that performs a specific task.
👉 Think of a function as a machine:
You give it some input (parameters).
It performs some process.
It may give back an output (return value).
✅ Why use functions?
Avoids repetition (write once, use many times).
Increases readability.
Helps in debugging (errors easier to find).
Supports modular programming (breaking large program into
small parts).
📌 Example (without function):
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
📌 Example (with function):
void printHello() {
printf("Hello\n");
}
int main() {
printHello();
printHello();
printHello();
return 0;
}
✅ Both give the same result, but the second is cleaner.
2️⃣ Types of Functions in C
C provides two types of functions:
1. Library Functions (Pre-defined)
o Already written by C developers.
o Available through header files.
o Examples:
printf() → prints text
scanf() → takes input
sqrt() → calculates square root
strlen() → finds string length
📌 Example:
#include <stdio.h>
#include <math.h>
int main() {
double result = sqrt(16);
printf("Square root: %lf", result);
return 0;
}
✅ Output: Square root: 4.000000
2. User-Defined Functions
o Written by the programmer.
o Used when we want to create our own reusable tasks.
📌 Example:
#include <stdio.h>
// function definition
void greet() {
printf("Welcome to C programming!\n");
}
int main() {
greet(); // function call
return 0;
}
✅ Output: Welcome to C programming!
3️⃣ Structure of a User-Defined Function
A function in C has four parts:
1. Function Declaration (Prototype)
o Tells the compiler about the function name, return type, and
parameters.
o Written before main().
👉 Syntax:
returnType functionName(parameter list);
📌 Example:
int add(int, int); // declaration
2. Function Definition
o Actual body of the function.
o Contains the code that runs when the function is called.
👉 Syntax:
returnType functionName(parameter list) {
// body
return value; // if returnType is not void
}
📌 Example:
int add(int a, int b) {
return a + b;
}
3. Function Call
o Tells the program to execute the function.
o Can be called from main() or another function.
📌 Example:
int result = add(5, 3); // calling add function
4. Return Statement
o If function is supposed to give back a value, use return.
o If nothing is returned, function return type should be void.
4️⃣ Example – Function with Parameters and
Return Value
#include <stdio.h>
// declaration
int add(int, int);
int main() {
int x = 10, y = 20, sum;
sum = add(x, y); // function call
printf("Sum = %d\n", sum);
return 0;
}
// definition
int add(int a, int b) {
return a + b; // returns sum
}
✅ Output:
Sum = 30
5️⃣ Types of User-Defined Functions
Depending on arguments (input) and return values (output), functions
can be:
1. No arguments, no return value
2. void greet() {
3. printf("Hello World\n");
4. }
5. Arguments, no return value
6. void greet(char name[]) {
7. printf("Hello %s\n", name);
8. }
9. No arguments, return value
10. int getNumber() {
11. return 10;
12. }
13. Arguments and return value
14. int square(int n) {
15. return n * n;
16. }
6️⃣ Difference Between Library Functions and
User-Defined Functions
Feature Library Functions User-Defined Functions
Definition Predefined in C libraries Written by programmer
Feature Library Functions User-Defined Functions
Availability Already available Created as per need
Examples printf(), scanf(), sqrt(), strlen() add(), multiply(), greet()
Flexibility Limited (fixed tasks) Fully flexible
Purpose Common tasks Custom tasks
7️⃣ Key Takeaways for Students
A function is a reusable block of code.
Two types: Library (built-in) and User-Defined (programmer’s
own).
Functions reduce repetition, improve readability, and make
debugging easier.
User-defined functions have declaration, definition, call, and
return.
Always choose functions to write modular programs instead of
long, repetitive code.