






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































Modular programming is a way of writing a program by dividing it into small parts called modules (functions). Each module performs a specific task and works independently.
👉 “Big program → small parts (modules/functions)”
A large program is broken into small functions Each function handles one task only All modules together form the complete program
Easy to understand program Divides program into small parts Easy to find errors Supports code reusability Easy to modify and maintain Improves program structure Reduces complexity Helps teamwork
A function is a block of code that performs a specific and well- defined task. It helps to avoid writing the same instructions again and again in a program.
Functions are used to reduce repetition of code and make programs easier to manage.
Instead of repeating the same statements many times, we can write them once inside a f
A function is a named group of statements that performs a particular task. It is a subpart of a program that can be reused multiple times.
These are predefined functions in C Provided by the compiler Example: printf(), scanf(), strlen()
These are created by the programmer Used to perform specific tasks as needed Example: sum(), factorial()
A function definition is where we write the actual code (statements) of a function.
return_type function_name(argument list);
int sum(int a, int b);
It is written before main() It informs the compiler about the function Helps in checking correct function usage
#include #include
float area(float); float area(float a) { float result = M_PI*a; return result; int main() { float radius = 10.5; float a = area(radius); printf("%f",a); return 0; }
Formal Parameters Actual Parameters
Used in function definition Used in function call
Act as variables (placeholders) Real values or variables
Receive data from caller Send data to function
Written inside function parentheses
Written inside function call
Exist only inside the function Exist in main or calling function
Example: int sum(int a, int b) Example: sum(5, 10)
Default arguments are values given to function parameters while defining the function. If the user does not pass a value, the default value is used.
👉 “If no value is given, use default value”
float i(float p, int t, float r = 0.24);
i(0.13, 6, 0.14) → r = 0.14 (user value used)
i(0.13, 6) → r = 0.24 (default value used)
Values of x and y are passed to the function The function processes these values Result is returned to the calling program
The return statement is used to exit from a function and send a value back to the main program.
👉 “Return means give result and go back”
return 0;
Function finishes its work return sends the result back Program control goes back to main function
A function can return only one value After return, function stops immediately return is used to give output from function
If a function does not return anything, we use void In that case:
return;
(or no return needed)
In pass by value, the value of the variable is passed to the function. A copy of the value is created inside the function. Changes made inside the function do not affect the original value.
x = 10 function gets 10 (copy) changes inside function → does NOT change original x
Original value is safe Works on copy of data No change in main function variable
In pass by reference, the address of the variable is passed to the function. The function works on the original value. Changes made inside the function affect the original variable.
x = 10 function gets address of x changes inside function → changes original x
#include
void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }
int main() { int a = 100, b = 200; printf("Before swapping: %d %d\n", a, b); swap(&a, &b); printf("After swapping: %d %d\n", a, b); return 0;
Function does not take input parameters Function does not return any value Input is taken inside the function itself
// function to read two numbers and print its sum void add() {
int a, b, sum; printf("Enter the values of a & b: "); scanf("%d%d", &a, &b);
sum = a + b; printf("Sum = %d", sum); }
2. Functions with arguments and no return
value
Function takes input parameters Function does not return any value Result is directly printed inside function
// function that takes two arguments and prints their sum
void add(int a, int b) { int sum; sum = a + b; printf("Sum = %d", sum); }
3. Functions with no arguments but return a
value
Function does not take arguments Function returns a value Input is taken inside function
// function to read two numbers and return sum int add() { int a, b, sum; printf("Enter the values of a & b: "); scanf("%d%d", &a, &b); sum = a + b; return sum; }
printf("Odd number"); }
Write a program to perform
arithmetic operations using
function
#include
int add(int a, int b) { return (a + b); } int diff(int a, int b) { return (a - b); } int mul(int a, int b) { return (a * b); } float div(int a, int b) { return (float)a / b; } int mod(int a, int b) { return (a % b); } void main() { int x, y; printf("Enter the values: "); scanf("%d%d", &x, &y);
printf("Sum = %d\n", add(x, y)); printf("Difference = %d\n", diff(x, y)); printf("Multiply = %d\n", mul(x, y)); printf("Division = %f\n", div(x, y)); printf("Modulo = %d\n", mod(x, y));
i n t c h e c k P r i m e ( i n t n ) { i n t i ; f o r ( i = 2 ; i < n ; i + + ) { i f ( n % i = = 0 ) r e t u r n 0 ; / / n o t p r i m e } r e t u r n 1 ; / / p r i m e } v o i d m a i n ( ) { i n t n , i ; p r i n t f ( " E n t e r t h e r a n g e : " ) ; s c a n f ( " % d " , & n ) ; p r i n t f ( " P r i m e n u m b e r s a r e : \ n " ) ; f o r ( i = 2 ; i < = n ; i + + ) { i f ( c h e c k P r i m e ( i ) ) p r i n t f ( " % d \ t " , i ) ; } }
A recursive function is a function in C that calls itself again and again to solve a problem, but with smaller and simpler values each time, until it reaches a condition where it stops.
This stopping point is called the base condition.
A function solves a problem Instead of solving everything at once, it breaks the problem into smaller parts It calls itself to solve each smaller part
if(n == 0) return 1; else return n * fact(n - 1); } void main() { int n, r; float nCr, nPr;
printf("Enter n and r: "); scanf("%d%d", &n, &r);
nPr = (float)fact(n) / fact(n - r); nCr = (float)fact(n) / (fact(r) * fact(n - r));
printf("nCr = %f\n", nCr); printf("nPr = %f", nPr); }
2. Sum of series: 1 + 1/2! + 1/3! +
#include
int fact(int n) { if(n == 0) return 1; else return n * fact(n - 1); }
void main() { int n, i; float sum = 0; printf("Enter limit: "); scanf("%d", &n); for(i = 1; i <= n; i++) sum = sum + 1.0 / fact(i);
printf("Sum = %f", sum); }
3. Fibonacci Series (Recursive)
#include
int fib(int n) { if(n == 1) return 0; else if(n == 2) return 1; else return fib(n-1) + fib(n-2); }
void main() { int n, i;
printf("Enter limit: "); scanf("%d", &n);
for(i = 1; i <= n; i++) printf("%d\t", fib(i)); }
4. Sum of digits (Recursive)
#include
int sod(int n) { if(n == 0) return 0; else return (n % 10) + sod(n / 10); }
void main() { int n; printf("Enter number: "); %
Rules
Pass only the array name in the function call Formal parameter must be an array No need to specify array size in function definition Function prototype should indicate array parameter
1. Sorting 1D Array using Function
#include
void sort(int a[], int n) { int i, j, temp; for(i = 0; i < n-1; i++) { for(j = 0; j < n-i-1; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } }
void main() { int a[20], n, i;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, n);
for(i = 0; i < n; i++) printf("%d ", a[i]); }
2. Row-wise Sorting of Matrix
#include
void sort(int a[], int n) { int i, j, temp;
for(i = 0; i < n-1; i++) { for(j = 0; j < n-i-1; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } }
void main() { int a[10][10], m, n, i, j;
scanf("%d%d", &m, &n);
for(i = 0; i < m; i++) for(j = 0; j < n; j++) scanf("%d", &a[i][j]);
for(i = 0; i < m; i++) sort(a[i], n);
for(i = 0; i < m; i++) { for(j = 0; j < n; j++) printf("%d ", a[i][j]); printf("\n"); } }