Module 4 Notes
Module 4 Notes
CHAPTER 1
1. Built-in functions: These are C language functions already available with C compliers and can be
used by any programmers.
Ex: printf( ),scanf( )
2. User-defined functions: These are written by programmers for their own purpose and are not readily
available.
Advantages of using Functions:
Functions based modular programming is advantageous in many ways:
1. Managing huge programs and software packages is easier by dividing them into
functions/modules—Maintenance is easier
2. Error detection is easier—Debugging is easier
3. Functions once written can be re-used in any other applications – Reusability is enhanced
4. We can protect our data from illegal users—Data protection becomes easier
The below block diagram represents how functions are useful: Instead of writing entire program within
main( ) function we can write independent modules as shown:
main( ) Subtract ( )
main( ) calls add( ) completes the work
and returns back to
main( )
A function can accept any number of inputs but, returns only one value as output as shown below:
2. MODULAR PROGRAMMING
Modular programming is defined as organizing a large program into small, independent program
segments called modules that are separately named and individually callable program units.
It is basically a “divide-and-conquer” approach to problem solving.
The characteristics of modular programming are:
1. Each module should do only one thing.
2. Communication between modules is allowed only by calling module.
3. No communication can take place directly between modules that do not have calling-called
relationship.
4. All modules are designed as single-entry, single-exit systems using control structures.
5. A module can be called by one and only higher module.
i. Function definition: It is an independent program module that is specially written to implement the
requirements of the function.
ii. Function call: The function should be invoked at a required place in the program which is called as
Function call.
The program/function that calls the function is referred as calling program or calling function.
The program/function that is called by program/function is referred as called program or called
function.
iii. Function declaration: The calling program should declare any function that is to be used later in the
program which is called as function declaration.
All the above six elements are grouped into two parts. Namely:
Function header (First three elements)
Function body (Second three elements)
Syntax:
function_type function_name(list of parameters)
{
local variable declaration;
executable statement1;
executable statement2;
......
......
return statement;
}
Note: The first line function_type function_name(parameter list) is known as the function header and the
statements within opening and closing braces is known as the function body.
i. Function Header: It consists of three parts: Function type, function name and list of paraemeters.
The semicolon is not present at the end of header.
Function type:
The function type specifies the type of value that the function is expected to return to the calling
function.
If the return type or function type is not specified, then it is assumed as int.
If function is not returning anything, then it is void.
Function name: The function name is any valid C identifier and must follow same rules as of other
variable.
List of Parameters: The parameter list declares the variables that will receive the data sent by the calling
program which serves as input and known as Formal Parameter List.
ii. Function Body: The function body contains the declarations and statements necessary for
performing the required task. The body is enclosed in braces, contains three parts:
FUNCTION DECLARATION
4. CATEGORIES OF FUNCTIONS
User defined
functions
Functions with
void functions
parameters and
without parameters
return values
When a function has no arguments, it does not receive any data from the calling function.
Similarly, when it does not return a value, the calling function does not receive any data from the called
function.
Example:
#include<stdio.h>
void add( );
main( )
{
add( );
}
void add( )
{
int a=5, b=5, sum=0;
sum = a+b;
printf(“Result = %d”, sum);
return;
}
The calling function accepts the input, checks its validity and pass it on to the called function.
The called function does not return any values back to calling function.
The actual and formal arguments should match in number, type and order.
Figure: Arguments matching between the function call and the called function
Example:
#include<stdio.h>
void add(int, int);
main( )
{
int a=5, b=5;
add(a, b);
}
void add(int a, int b )
{
int sum=0;
sum = a+b;
printf(“Result = %d”, sum);
return;
}
3. Functions with parameters and return values – Arguments with return values
The calling function sends the data to called function and also accepts the return values from called
function.
#include<stdio.h>
int add(int, int);
main( )
{
int a=5, b=5,sum=0 ;
sum = add(a, b);
printf(“Result = %d”, sum);
}
int add(int a, int b)
{
int x;
x = a+b;
return x;
}
4. Functions without parameters and with return values – No arguments but return values
The calling function does not send any data to called function and but, accepts the return values from
called function.
Example:
#include<stdio.h>
int add( );
main( )
{
int sum=0 ;
sum = add( );
printf(“Result = %d”, sum);
}
int add( )
{
int a=5, b=5, x=0;
x = a+b;
return x;
}
Actual Parameters: When a function is called, the values that are passed in the call are called actual
parameters.
Formal Parameters: The values which are received by the function, and are assigned to formal
parameters.
Global Variables: These are declared outside any function, and they can be accessed on any function in
the program.
Local Variables: These are declared inside a function, and can be used only inside that function.
5. INTER-FUNCTION COMMUNICATION
Two functions can communicate with each other by two methods:
1. Pass by value (By Passing parameters as values)
2. Pass by address (By passing parameters as address)
1. Pass by value:
In pass-by-value the calling function sends the copy of parameters (Actual Parameters) to the called
function (Formal Parameters). The changes does not affect the actual value.
Example:
#include<stdio.h>
int swap(int, int);
int main( )
{
int a=5, b=10 ;
swap(a, b);
printf(“%d%d:, a, b);
}
int swap(int x, int y)
{
int temp;;
temp = x;
x = y;
y = temp;
}
In the above example even though x and y values get swapped, the a and b values remains unchanged.
So swapping of two values in this method fails.
In pass by reference the calling function sends the address of parameters (Actual Parameters) to the
called function (Formal Parameters). The changes affects the actual value.
Example:
#include<stdio.h>
int swap(int *, int *);
int main( )
{
int a=5, b=10 ;
swap(&a, &b);
printf(“%d%d:, a, b);
}
int swap(int *x, int *y)
{
int temp;;
temp = *x;
*x = *y;
*y = temp;
}
6. NESTING OF FUNCTIONS
Example:
#include<stdio.h>
float checkno(float );
float div(float, float);
main( )
{
float a, b, res;
printf(“Enter two numbers”);
scanf(“%f%f”,&a,&b);
div(a,b);
Prof. Suvika K V, Dept. of CSE, CBIT, Kolar 2020-21
8
CPPS – 18CPS13 Module 4
}
float div(float a, float b)
{
if(checkno(b))
printf(“Result = %f”, a/b);
else
printf(“Division not possible”);
}
float checkno(float b)
{
if(b!=0)
return 1;
else
return 0;
}
#include<stdio.h> #include<stdio.h>
int square(int); int sum(int [ ]);
int main( ) int main( )
{ {
int num[5], i; int marks[5], i;
num[5] ={1, 2, 3, 4, 5}; marks[5] ={10, 20, 30, 40, 50};
for(i=0; i<5; i++) sum(marks);
{ }
square(num[i]); int sum(int n[ ])
} {
} int i, sum=0;
int square(int n) for(i=0; i<5; i++)
{ {
int sq; sum = sum+n[i];
sq= n * n; }
printf(“%d ”, sq); printf(“Sum = %d ”, sum);
} }
CHAPTER 2
RECURSION
Programmers use two approaches to write repetitive algorithms:
Limitations of Recursion
Recursive solutions may involve extensive overhead because they use function calls.
Each time you make a call, you use up some of your memory allocation. If recursion is deep, then
you may run out of memory.
PROGRAMS
1. WACP to find a factorial of number using recursion.
#include<stdio.h>
int fact(int);
void main( )
{
int n,res;
printf(“Enter a number”);
scanf(“%d”,&n);
res=fact(n);
printf(“Factorial = %d”,res);
}
int fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
1 WAP to find GCD and LCM of two numbers using concept of functions.
#include<stdio.h>
int hcf(int, int);
int main( )
{
int x, y, gcd, lcm;
printf(“Enter two numbers”);
scanf(“%d%d”, &x, &y);
gcd = hcf(x, y);
lcm = (x * y)/ gcd;
printf(“GCD = %d”, gcd);
printf(“LCM = %d”, lcm);
}
int hcf( int x, int y)
{
if(x == 0)
return y;
while(y!=0)
{
if(x > y)
x = x-y;
else
y = y-x;
}
return x;
}
#include<stdio.h>
int factorial(int);
int main( )
{
int n, r, binom;
printf(“Enter value of n and r”);
scanf(“%d%d”, &n, &r);
if(n<0 || r<0 || n<r)
printf(“Invalid Input”);
else
{
binom = factorial(n)/ factorial® * factorial(n-r);
printf(“Result = %d”, binom);
}
}
int factorial(int n)
{
if(n == 0)
return 1;
else
return(n*factorial(n-1));
}
4 Write a C recursive function for multiplying two integers where a function call is passed with
two integers m and n.
#include<stdio.h>
int product(int, int);
int main( )
{
int m, n, res;
printf(“Enter two numbers”);
scanf(“%d%d”, &m, &n);
res = product(m, n);
printf(“Result = %d”, res);
}
int product(int m, int n)
{
if(m<n)
return product(n, m);
else if(n!=0)
return (m + product(m, n-1));
else
return 0;
}
5 Differentiate:
i) User defined and Built-in function
ii) ii) Recursion and Iteration
Iteration Recursion
Allows set of instructions to be repeatedly The statement in a body of function calls itself.
executed.
Initialization, Condition and Updation are Only termination condition is specified.
present.
Applicable to iterative statements. Applicable to functions.
The code size is bigger. The code size is reduced.
No overhead of repeated function calls. Processes overhead of repeated function calls.
#include<stdio.h>
void main( )
{
int n, a[100], i, large=0;
printf(“Enter the size of array”);
scanf(“%d”, &n);
printf(“Enter the elements of array”);
for(i=0; i<n; i++)
scanf(“%d”, &a[i]);
for(i=0; i<n; i++)
{
if(a[i] > large)
large = a[i];
}
Printf(“Largest number = %d”, large);
}
7 WACP using functions to swap two numbers using global variable concept and call by
reference concept.
#include<stdio.h>
int a, b;
void swap(int *, int *);
int main( )
{
printf(“Enter two numbers”);
scanf(“%d%d”, &a, %b);
printf(“Before Swapping a = %d, b= %d”, a, b);
swap(&a, &b);
printf(“After Swapping a = %d, b= %d”, a, b);
}