KEMBAR78
PPS Unit 4 Updated Notes | PDF | Parameter (Computer Programming) | Variable (Computer Science)
0% found this document useful (0 votes)
35 views19 pages

PPS Unit 4 Updated Notes

This document contains lecture notes for Unit 4 of the Programming for Problem Solving course at Meerut Institute of Technology, focusing on functions in C programming. It covers topics such as types of functions, parameter passing methods (call by value and call by reference), recursion, and the scope of variables, along with examples and questions for practice. The notes aim to enhance understanding of functions, their definitions, declarations, and advantages in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views19 pages

PPS Unit 4 Updated Notes

This document contains lecture notes for Unit 4 of the Programming for Problem Solving course at Meerut Institute of Technology, focusing on functions in C programming. It covers topics such as types of functions, parameter passing methods (call by value and call by reference), recursion, and the scope of variables, along with examples and questions for practice. The notes aim to enhance understanding of functions, their definitions, declarations, and advantages in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Meerut Institute of Technology, Meerut

Programming for problem Solving (BCS101/BCS201)


Lecture Notes (UNIT-4) Ver.2024.02

Unit –IV

Sr. No. of Topics/Sub Topics CO


No. Periods Covered
Functions: Introduction, Types of Functions, Functions with Array, Passing
Parameters to Functions, Call by Value, Call by Reference, Recursive Functions.
Basic of searching and Sorting Algorithms: Searching & Sorting Algorithms (
Linear Search , Binary search , Bubble Sort, Insertion and Selection Sort)
1. 1 Introduction, Types of Functions, CO-3

2. 1 Functions with Array CO-3


3. 1 Passing Parameters to Functions, Call by Value, Call CO-3
by Reference,

4. 1 Recursive Functions CO-3


5. 1 Searching & Sorting Algorithms CO-4
6. 1 Searching & Sorting Algorithms CO-4
7. 1 Searching & Sorting Algorithms CO-4
8 1 Searching & Sorting Algorithms CO-4

CO1 : To understand the fundamental of computer, problem solving & C programming


CO2 : To apply various control statements
CO3 : To utilize the concept of functions
CO4 : To apply the concept of primitive & non primitive data types
CO5 : To make use of file handling & preprocessors
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02

FUNCTIONS IN C

Ques) What do you mean by functions in C? Also write its advantages.(2017-18)

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.

Advantages of using functions

a) To improve the readability of code.


b) Improves the reusability of the code, same function can be used in any program rather than writing the
same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by function calls.

Ques) How many types of functions are there in C language?

C language has two types of functions

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

Ques) Write a program in C to find sum of two numbers using function.

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

The function definition contains the following two parts:

 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.

Ques) Explain syntax of function call. .(2019-20, 2017-18, 2005-06)

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

func_name(arg1, arg2, arg3 ...)


Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02

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.

Ques) Explain syntax of function declaration. .(2019-20, 2017-18, 2005-06)

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.

1. Name of the function

2. Number and type of arguments received by the function.

3. Type of value returned by the function

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-

return_type func_name(type1 arg1, type2 arg2, .......);

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

1) What is the need of function?


2) What is the meaning of prototype of a function?
3) Explain the difference between function declaration and definition of a function
4) What are functions? What are the advantages to use multiple functions in a program?
5) Explain function declaration and definition of a function with example.
6) Write a function in C language to find the reverse of a given integer number.
7) Explain function prototype? Why is it required?

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.

 They are used in the function call.


 They are actual values that are passed to the function definition through the function call.
 They can be constant values or variable names (such as local or global).

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

 They are used in the function header.


 They are used to receive the values that are passed to the function through function call.
 They are treated as local variables of a function in which they are used in the function header.

Ques) Write short note on return statement.


OR
What is the utility of return statement in C function?(2012-13)

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?

TYPES OF PARAMETER PAASING

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 Value Example - Swapping 2 numbers using Call by Value

void swap(int, int);


int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}

void swap(int a, int b)


{
int temp;
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
}

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.

/Call by Reference Example - Swapping 2 numbers using Call by Value


Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02

void swap(int*, int*);


int main()
{ int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp; }

SCOPE OF A VARIABLE

Ques) Explain the scope of a variable.(2016-17,2014-15)

There are two types of 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.

How recursion works?

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.

More programs related to recursion

Problem 1: Factorial of a Number Using Problem 1: Program to print fibonacci series


Recursion(2016-17, 2015-16, 2014-15) using recursion.(2021-22, 2017-18, 2016-17,
2013-14)
#include<stdio.h> #include <stdio.h>
long fact (int n); int fibonacci(int i)
main ( ) {
{ if(i == 0)
int num; return 0;
printf ("Enter a number ") ;
scanf("%d",&num); else if(i == 1)
printf ("Factorial of %d is %ld\n", num, fact (num) ) ; return 1;
}
long fact (int n) else
{ return fibonacci(i-1) + fibonacci(i-2);
if (n==0) }
return(1) ; int main()
else {
return(n*fact(n-1)); int i;
} for (i = 0; i < 10; i++)
{
printf("%d\t\n", fibonacci(i));
}
return 0; }

Ques)What are the advantages and disadvantages of recursion?(2016,17, 2012-13)

Advantages and Disadvantages of Recursion


Advantages:
 Reduce unnecessary calling of function.
 Through Recursion one can solve problems in easy way while its iterative solution is very big and complex.
Disadvantages:
 Recursive solution is always logical and it is very difficult to trace. (Debug and understand).
 In recursive we must have if statement somewhere to force the function to return without the recursive calls
being executed, otherwise the function will never return.
 Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC.
 Recursion uses more processor time.
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02

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

2)Write a recursive function to find sum of digits of a given number.

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) What is searching? Write a program to implement linear search. ( 2021-22)


OR
Explain linear search and binary search techniques for searching an element.
Searching is a process of locating a particular element present in a given set of elements. The element
may be a record, a table, or a file.
A search algorithm is an algorithm that accepts an argument „a‟ and tries to find an element whose value
is „a‟. It is possible that the search for a particular element in a set is unsuccessful if that element does not
exist. There are two basic techniques available for searching.
1) Linear Search (Sequential Search)
2) Binary Search

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

/* Program to search an element from an array (Linear search)*/

# 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

Worst Case Complexity of Linear Search = O(n)

Ques) Write short note on binary search.


OR
Program to search an element from an array using binary search.

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

/* Program for Binary Search*/

#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

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}

Output:

Enter number of elements


7
Enter 7 integers
-4
5
8
9
11
43
485
Enter value to find
11
11 found at location 5

Worst Case Complexity of Linear Search = O(log n)

SORTING

Ques) What is a sorting techniques? (2018-19, 2017-18)

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.*/

#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++)
{
for(j=i+1;j<SIZE;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf ("Sorted array is”);
Meerut Institute of Technology, Meerut
Programming for problem Solving (BCS101/BCS201)
Lecture Notes (UNIT-4) Ver.2024.02

for(i=0;i<SIZE;i++)
printf("%d ",arr[i]);
printf (" \n"); }

Worst Case Complexity of Selection sort = O(n2)

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

Worst Case Complexity of Bubble sort = O(n2)

Ques) Explain insertion sort concept and write the program.


OR
Implement sorting technique using bubble sort on the following sequence:
34, 78, 12, 5, 3, 98, 101, 15

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)

You might also like