Functions in C
A function in C is a block of code written to perform a specific task.
Functions improve code reusability, readability, and make programs easier to debug and maintain.
What is a Function?
A function is a group of statements combined to perform an operation. C supports two types of functions:
- Library Functions β built-in (printf, scanf, strlen, etc.)
- User-defined Functions β created by the programmer
Why Use Functions?
- Makes code modular
- Avoids repeating the same code
- Easier debugging & maintenance
- Improves readability
- Enables teamwork by dividing work
Parts of a Function
- Function Declaration (prototype)
- Function Definition (actual code)
- Function Call (using the function)
Syntax of a Function
return_type function_name(parameter_list) {
// body of function
}
- return_type β type of value returned (int, float, void, etc.)
- function_name β any valid C identifier
- parameter_list β input values (optional)
Function Declaration (Prototype)
A prototype tells the compiler the function name, return type, and parameters.
int add(int, int); void display(); float avg(float x, float y);
Function Definition
This is where the function's actual logic is written.
int add(int a, int b) {
return a + b;
}
Function Call
A function is executed when it is called.
int result = add(5, 7);
Types of Functions Based on Arguments & Return Value
- 1. No arguments, no return value
- 2. Arguments, no return value
- 3. No arguments, return value
- 4. Arguments and return value
π void means βno value is returnedβ.
π You can create unlimited user-defined functions.
Scope of Variables in Functions
- Local Variables β inside function, destroyed after execution
- Global Variables β declared outside, accessible everywhere
- Static Variables β retain value between function calls
Function Advantages
- Less code duplication
- Improved structure
- Better testing
- Easy code distribution among team
- Reusable in large applications
Function Examples
1. Function with No Arguments & No Return Value
#include <stdio.h>
void greet(){
printf("Hello, Welcome!");
}
int main(){
greet();
return 0;
}
2. Function with Arguments & No Return Value
#include <stdio.h>
void show(int a, int b){
printf("Values: %d, %d", a, b);
}
int main(){
show(10, 20);
return 0;
}
3. Function with No Arguments & Return Value
#include <stdio.h>
int give(){
return 100;
}
int main(){
printf("%d", give());
return 0;
}
4. Function with Arguments and Return Value
#include <stdio.h>
int add(int x, int y){
return x + y;
}
int main(){
printf("Sum = %d", add(5, 9));
return 0;
}
5. Function to Find Maximum
#include <stdio.h>
int max(int a, int b){
return (a > b) ? a : b;
}
int main(){
printf("%d", max(7, 3));
return 0;
}
6. Function Returning a Character
#include <stdio.h>
char grade(int m){
if(m >= 90) return 'A';
return 'B';
}
int main(){
printf("Grade = %c", grade(92));
return 0;
}
7. Function to Calculate Square
#include <stdio.h>
int square(int n){
return n * n;
}
int main(){
printf("%d", square(6));
return 0;
}
8. Function for Multiplication
#include <stdio.h>
int multiply(int a, int b){
return a * b;
}
int main(){
printf("%d", multiply(4, 9));
return 0;
}
9. Even or Odd Function
#include <stdio.h>
int isEven(int n){
return n % 2 == 0;
}
int main(){
printf("%d", isEven(7));
return 0;
}
10. Function to Print a Line
#include <stdio.h>
void line(){
printf("--------------\n");
}
int main(){
line();
return 0;
}
11. Function to Swap Two Numbers (Call by Value)
#include <stdio.h>
void swap(int a, int b){
int temp = a;
a = b;
b = temp;
printf("%d %d", a, b);
}
int main(){
swap(10, 20);
return 0;
}
12. Function to Count Digits
#include <stdio.h>
int digits(int n){
int c = 0;
while(n != 0){
c++;
n /= 10;
}
return c;
}
int main(){
printf("%d", digits(672));
return 0;
}
13. Function to Reverse a Number
#include <stdio.h>
int reverse(int n){
int r = 0;
while(n != 0){
r = r * 10 + n % 10;
n /= 10;
}
return r;
}
int main(){
printf("%d", reverse(1234));
return 0;
}
14. Function to Calculate Factorial
#include <stdio.h>
int fact(int n){
int f = 1;
for(int i=1; i<=n; i++)
f *= i;
return f;
}
int main(){
printf("%d", fact(6));
return 0;
}
15. Function Returning Float
#include <stdio.h>
float average(float a, float b){
return (a + b)/2;
}
int main(){
printf("%.2f", average(10, 20));
return 0;
}
16. Function for Simple Interest
#include <stdio.h>
float si(float p, float r, float t){
return (p * r * t) / 100;
}
int main(){
printf("%.2f", si(1000, 5, 2));
return 0;
}
17. Function with Array Argument
#include <stdio.h>
int sum(int arr[], int n){
int s = 0;
for(int i=0; i< n; i++)
s += arr[i];
return s;
}
int main(){
int a[] = {1,2,3,4,5};
printf("%d", sum(a, 5));
return 0;
}
18. Function Returning Array Element
#include <stdio.h>
int last(int arr[], int n){
return arr[n-1];
}
int main(){
int a[] = {4, 7, 9};
printf("%d", last(a, 3));
return 0;
}
19. Function to Count Vowels in String
#include <stdio.h>
int vowels(char *s){
int c = 0;
for(int i=0; s[i]; i++){
char ch = s[i];
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
c++;
}
return c;
}
int main(){
printf("%d", vowels("Sourav Sahu"));
return 0;
}
20. Function Calling Another Function
#include <stdio.h>
int square(int n){
return n*n;
}
int cube(int n){
return n * square(n);
}
int main(){
printf("%d", cube(4));
return 0;
}
Real Life Uses of Functions
- Menu-driven programs
- Calculators
- ATM operations
- Game modules (movement, attack, score)
- Student record management
- Mathematical computations
Practice Questions
- What is a function? Explain with syntax.
- Give differences between library and user-defined functions.
- Explain the purpose of a function prototype.
- Write the types of functions based on return type & parameters.
- Explain scope of local and global variables.
Practice Task
Create a program with three user-defined functions:
input(), calculate(), display().
The program should take 2 numbers and show their sum, difference, and product.