PPS Unit 4 Updated Notes
PPS Unit 4 Updated Notes
Unit –IV
FUNCTIONS IN C
A function is a self-contained subprogram that is meant to do some specific, well-defined task. A program
consists of one or more functions. If a program has only one function then it must be the main function.
1. Library functions
2. User-defined functions
Library Functions: These functions are present in the C library and they are predefined. For example sqrt( )
is a mathematical library function which is used for finding out the square root of any number. The functions
scanf( ) and printf( ) are input output library functions. Similarly we have functions like strlen( ), strcmp( ) for
string manipulation.
User Defined Functions: Users can create their own functions for performing any specific task of the
program. These types of functions are called user-defined functions. To create and use these functions, we
should know about these three things
1. Function definition
2. Function declaration
3. Function call
#include<stdio.h>
int sum(int x, inty);/*Function declaration*/
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
main ( )
{
int a,b, s;
printf ("Enter values for a and b”);
scanf ("%d %d", &a, &b) ;
s=sum (a, b); /*Function call*/
printf ("Sum of %d and %d is %d \ n" ,a, b, s) ;
}
int sum (int x, int y) /*Function definition*/
{
int s;
S=x+y;
return s;
}
Ques) Explain syntax of function definition.(2019-20, 2017-18, 2005-06)
Function Definition: The function definition consists of the whole description and code of a function. It tells
what the function is doing and what are its inputs and outputs. A function definition consists of two parts - a
function header and a function body.
The general syntax of a function definition is
return_type func_name ( type1 argl, type2 arg2,…… )
{
local variables declarations;
statement;
return(expression) ;
}
Function Header: The first line of function definition is known as function header. It consists of three parts:
return type, function name and argument list(formal arguments).
Function Body: It consists of three parts: local variable declaration part, executable part and return statement.
All the required variables are declared at the local variable declaration part. Executable part contains all the
executable statements that performs actual task. Return statement should be the last statement inside the
function body. It is optional. If return type of function is void then it can be omitted but function
having return type other than void must include return statement.
Function Call: The function definition describes what a function can do, but to actually use it in the program
the function should be called somewhere. A function is called by simply writing its name followed by the
argument list inside the parentheses
These arguments arg 1, arg2, ,...are called actual arguments. Here func_name is known as the called
function while the function in which this function call is placed is known as the calling function. For example
in the above program, main is the calling function and sum () is the called function and a, b are actual
arguments.
Function Declaration: The calling function needs information about the called function. If definition of the
called function placed before the calling function, then declaration is not needed.
The function declaration is also known as the function prototype and it informs the compiler about the
following three things.
Function declaration tells the compiler that a function with these features will be defined and used later in
the program. The general syntax of a function declaration is-
WAP to check whether an input number is even or odd WAP to find maximum between two numbers using
using function.(2012-13) function.(2012-13,2014-15)
# include<stdio.h> # include<stdio.h>
void find( int n); int max (intx, int y);
main() main( )
{ {
int n; int a, b;
printf ("Enter a number”); printf ("Enter two numbers”);
scanf("%d",&n); scanf("%d%d",&a,&b);
find(n); printf("Maximum of %d and %d is
return 0; % %d\n",a,b,max(a,b));
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
} return 0;
void find(int n) }
{ int max(int x, int y)
if (n%2==0) {
printf ("%d is even”,n); if (x>y)
else return x;
printf ("%d is odd”,n); else
} return y;
}
Questions
TYPES OF ARGUMENTS
Ques) Differentiate between between actual and formal arguments.( 2017-18, 2014-15,2007-08)
Actual Parameter: The parameters used in the function call are called actual parameters. These are the
actual values that are passed to the function. The actual parameters may be in the form of constant values
or variables. The data types of actual parameters must match with the corresponding data types of formal
parameters (variables) in the function definition.
Formal Parameters:
The parameters used in the header of function definition are called formal parameters of the function.
These parameters are used to receive values from the calling function.
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
A return statement ends the execution of a function, and returns control to the calling function. Execution
resumes in the calling function at the point immediately following the call. A return statement can return a
value to the calling function.
Syntax:
return expressionopt ;
The value of expression, if present, is returned to the calling function. If expression is omitted, the return
value of the function is undefined. The expression, if present, is evaluated and then converted to the type
returned by the function. When a return statement contains an expression in functions that have
a void return type, the compiler generates a warning, and the expression isn't evaluated.
Questions
1) Write a C program to add first seven terms of the following series using for loop. 1/1! +2/2! +3/3! +------(2016-17)
2) Differentiate between -actual and formal arguments.
3) What is the utility of return statement in C function?
Ques) Explain call by value and call by reference mechanism for function call using proper example.
OR
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
What do you mean by call by value and call by reference? Write an algorithm for swapping two numbers
using call by reference technique. Also write a C program for the above stated algorithm.
OR
What do mean by passing parameters? Discuss various types of parameter passing mechanism in C with
example.(2021-20,2019-20, 2018-19,2017-18, 2015-16,2014-15, 2011-12, 2010-11)
Parameter Passing Methods: While calling a function, there are two ways in which arguments can be
passed to a function –
1. Call by value: This method copies the actual value of an argument into the formal parameter of the function.
In this case, changes made to the parameter inside the function have no effect on the argument. By default, C
uses call by value to pass arguments. In general, it means the code within a function cannot alter the
arguments used to call the function.
Call by reference: This method copies the address of an argument into the formal parameter. Inside the
function, the address is used to access the actual argument used in the call. This means that changes made to
the formal parameter changes the actual parameter too.
SCOPE OF A VARIABLE
Local scope: The variables that are defined within the body of a function or a block, are local to that function
or block only and are called local variables. For example
func( )
{
int a, b;
…..
…..
}
Here a and b are local variables which are defined within the body of the function func( ). Local variables
can be used only in those functions or blocks, in which they are declared.
Global scope: The variables that are defined outside any function are called global variables. All functions in
the program can access and modify global variables. It is useful to declare a variable global if it is to be used by
many functions in the program. Global variables are automatically initialized to 0 at the time of declaration.
Questions
1)Write a function in C to exchange the content of two integer variables without using a third variable.
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
2)Explain the difference between parameter passing mechanism call by value and call by reference. Which is more efficient
and why? 3)What do you mean by call by value and call by reference? Write an algorithm for swapping two numbers using
call by reference technique. Also write a C program for the above stated algorithm.
4)What is a function? Why programmers use functions in code? While executing a function, how the values are passed
between „calling and called environment?
5)What do you mean by parameter passing? Discuss various types of parameter passing mechanism in C with examples
RECURSION
Ques) What is a recursive function? What are its basic properties? (2021-22,2019-20,2017-18, 2016-17,
2013-14, 2010-11, 2012-13, 2009-10, 2005-06)
Recursion: The function that calls itself (inside function body} again and again is known as recursive function.
In recursion the calling function and the called function are same.
void recurse()
{ ... .. ...
recurse();
... .. ...
}
int main()
{ ... .. ...
recurse();
... .. ...
}
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
The recursion continues until some condition is met to prevent it. To prevent infinite recursion, if...else
statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.
Questions
1)What is recursion? Write a program to print the Fibonacci series using recursion. Write a recursive function in C, which
takes an input from user to calculate a factorial using the recursion concept
3)A five digit positive integer is entered through the keyboard. Write a C function to calculate sum of digits of the 5-digit
number : (i) Without using recursion. (ii) Using recursion.(2016-17)
4)What is a function? Why programmers use functions in code? While executing a function, how the values are passed
between „calling and called environment?
SEARCHING
Ques) Program to search an element from an array (Linear search). (2020-21, 20119-
20, 2018-19)
1) Linear Search: n this, the list or array is traversed sequentially and every element is checked.
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
# include<stdio.h>
#define SIZE 10
main ( )
{
int i,arr[SIZE];
for(i=0;i<SIZE;i++)
{
printf ("Enter the value for arr[ %d]”,i+1);
scanf("%d",&arr[i)) ;
}
int item,flag=0;
printf ("Enter the item to be searched ") ;
scanf("%d",&item);
for(i=0;i<SIZE;i++)
{
if(item==arr[i])
{
flag=1;
break;
}
}
if(flag==1)
printf(“Search is successful and locationis %d”, i+1);
else
printf(“Search is unsuccessful”);
}
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
Output:
Enter the value for arr[ 1] 2
Enter the value for arr[ 2]4
Enter the value for arr[ 3]6
Enter the value for arr[ 4]3
Enter the value for arr[ 5]9
Enter the value for arr[ 6]5
Enter the value for arr[ 7] 11
Enter the value for arr[ 8]1
Enter the value for arr[ 9]8
Enter the value for arr[ 10]7
Enter the item to be searched13
Search is unsuccessful
2) Binary Search: These algorithms are specifically designed for searching in sorted data
structures. These type of searching algorithms are much more efficient than Linear Search as they
repeatedly target the center of the search structure and divide the search space in half.
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
Output:
SORTING
Sorting: Sorting is a technique to arrange the elements of a list in ascending or descending order.
There three basic algorithms to sort an array:
1) Selection sort
2) Bubble sort
3) Insertion sort
Ques) Explain bubble sort concept and write the program. (2021-22, 2017-18)
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
1) Selection sort: In Selection sort, the smallest element is exchanged with the first element
o the unsorted list of elements (the exchanged element takes the place where smallest element is initially
placed). Then the second smallest element is exchanged with the second element of the unsorted list of
elements and so on until all the elements are sorted.
/* Program to sort the elements of a 1-D array, in ascending order through selection sort.*/
for(i=0;i<SIZE;i++)
printf("%d ",arr[i]);
printf (" \n"); }
Ques) Explain bubble sort concept and write the program. (2021-22, 2019-20, 2018-19,
2017-18)
OR
Implement sorting technique using bubble sort on the following sequence: (2021-22)
34, 78, 12, 5, 3, 98, 101, 15
2) Bubble sort: Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
/* Program to sort the elements of a 1-D array, in ascending order through bubble sort.*/
#include <stdio.h>
#define SIZE 10
main( )
{
int arr [SIZE] ;
int i , j , temp;
printf ("Enter elements of the array : \n") ;
for(i=0;i<SIZE;i++)
scanf("%d",&arr[i]);
for(i=0;i<SIZE-1;i++)
{
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
for(j=0;j<SIZE-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf ("Sorted array is”);
for(i=0;i<SIZE;i++)
printf("%d ",arr[i]);
printf (" \n");
}
1) Insertion sort: Insertion sort is a simple sorting algorithm that works the way we sort playing
cards in our hands.
/* Program to sort the elements of a 1-D array, in ascending order through insertion sort.*/
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02
#include <stdio.h>
#define SIZE 10
main( )
{
int arr [SIZE] ;
int i ,k;
printf ("Enter elements of the array : \n") ;
for(i=0;i<SIZE;i++)
scanf("%d",&arr[i]);
for(k=1;k<SIZE;k++)
{
item=arr[k]; /*item is to be inserted at proper place*/
for(i=k-1; item<arr[i]&&i>=0 ;i- -)
arr[i+1]=arr[i] ;
arr[i+l]=item;
}
printf ("Sorted array is : \n") ;
for(i=0;i<SIZE;i++)
printf("%d ",arr[i]);
printf (" \n") ;
}
Worst Case Complexity of Insertion sort = O(n2)