Docsity

MODULE 4 C PROGRAMMING (2024 SCHEME KTU), Summaries of C programming

Toggle sidebar
bg1
MODULAR PROGRAMMING
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
ADVANTAGES
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
FUNCTIONS
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.
Why Functions are Used
Functions are used to reduce repetition of code and make
programs easier to manage.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download MODULE 4 C PROGRAMMING (2024 SCHEME KTU) and more Summaries C programming in PDF only on Docsity!

MODULAR PROGRAMMING

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

ADVANTAGES

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

FUNCTIONS

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.

Why Functions are Used

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.

Types of Functions in C

1. Library Functions

These are predefined functions in C Provided by the compiler Example: printf(), scanf(), strlen()

2. User-defined Functions

These are created by the programmer Used to perform specific tasks as needed Example: sum(), factorial()

Advantages of Using Functions

Code Reusability

Reduces Program Size

Easy to Understand

Easy Debugging

Easy Maintenance

Improves Modularity

Saves Time

Improves Program Structure.

FUNCTION DEFINITION

A function definition is where we write the actual code (statements) of a function.

return_type function_name(argument list);

Example

int sum(int a, int b);

Explanation

It is written before main() It informs the compiler about the function Helps in checking correct function usage

Program to calculate area of a

circle

#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 AND

ACTUAL PARAMETERS

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

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”

Example

float i(float p, int t, float r = 0.24);

How it works

i(0.13, 6, 0.14) → r = 0.14 (user value used)

i(0.13, 6) → r = 0.24 (default value used)

Important Point

Values of x and y are passed to the function The function processes these values Result is returned to the calling program

RETURNING FROM A

FUNCTION

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”

Example

return 0;

How it works

Function finishes its work return sends the result back Program control goes back to main function

Important Points

A function can return only one value After return, function stops immediately return is used to give output from function

Void Function

If a function does not return anything, we use void In that case:

return;

(or no return needed)

PASS BY VALUE vs PASS BY

REFERENCE

1. Pass by Value

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.

Example Idea

x = 10 function gets 10 (copy) changes inside function → does NOT change original x

Key Points

Original value is safe Works on copy of data No change in main function variable

2. Pass by Reference

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.

Example Idea

x = 10 function gets address of x changes inside function → changes original x

Swap Using Call by Reference

(Simple Program)

#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;

TYPES OF USER-DEFINED

FUNCTIONS

1. Functions with no arguments and no return

value

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));

Program: Prime numbers using function

i n c l u d e < s t d i o. h >

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 ) ; } }

Recursive Function

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"); } }