SlideShare a Scribd company logo
1 of 61
Unit– III
Functions
INTRODUCTION
 It is difficult to prepare and maintain a large-sized program.
 Moreover, the identification of the flow of data is hard to
understand.
 The best way to prepare a programming application is to divide the
bigger program into small pieces or modules and the same modules
can be repeatedly called from the main() function as and when
required.
INTRODUCTION (CONT..)
 These small modules or subprograms are easily manageable.
 This method is called the divide and conquer method.
 In C, such small modules are called functions.
 The programs written in the C language are highly dependent on
functions.
 The C program is nothing but a combination of one or more
functions.
INTRODUCTION (CONT..)
 A program divided in multiple
functions
INTRODUCTION (CONT..)
 Every C program starts with the user-defined function main().
 Each time when a new program is started, main() function must be
defined.
 The main() function calls another functions to share the work.
 The main program can supply data to a function.
 A specific operation is performed on the data and a value is returned
to the calling function.
INTRODUCTION (CONT..)
 The C language supports two types of functions:
 (1) library functions and
 (2) user-defined functions.
INTRODUCTION (CONT..)
(1) library functions :
 The library functions are pre-defined set of functions. Their task is
limited.
 One can only use the functions but cannot change or modify them.
For example, sqrt (81) gives result 9.
 The user do not know the internal working of function
INTRODUCTION (CONT..)
(2) user-defined functions:
 The functions defined by the user according to his/her requirement
are called user-defined functions.
 A user can modify the functions according to the requirement.
 A user certainly understands the internal working of the function.
 The user has full scope to implement his/her own ideas in the
function.
 Thus, the set of such user-defined functions can be useful to another
programmer.
INTRODUCTION (CONT..)
 One should include the file in which the user-defined functions are
stored to call the function in the program.
 For example, let square(9) be a user-defined function that gives the
result 81.
 Here, the user knows the internal working of the square() function,
as its source code is visible.
 This is the major difference between the two types of functions.
BASICS OF A FUNCTION
 A function is a self-contained block or a sub-program of one or more
statements that perform a special task when called.
 Why Use Functions?
1. If we want to perform a task repetitively, then it is not necessary to re-write the
particular block of the program again and again. Shift the particular block of
statements in a user-defined function. The function defined can be called any
number of times to perform the task.
2. Using functions, large programs can be reduced to smaller ones. It is easy to
debug and find out the errors in it. It also increases readability.
BASICS OF A FUNCTION (CONT..)
 How a Function Works?
1. Once a function is defined and called, it takes some data from the
calling function and returns a value to the called function.
2. The detail of inner working of a function is unknown to the rest of
the program. Whenever a function is called, control passes to the
called function and working of calling function is paused. When the
execution of a called function is completed, control returns back to
the calling function and executes the next statement.
BASICS OF A FUNCTION (CONT..)
 How a Function Works? (cont..)
3. The values of actual arguments passed by the calling function are
received by the formal arguments of the called function. The
number of actual and formal arguments should be the same. Extra
arguments are discarded if they are defined. If the formal
arguments are more than the actual arguments, then the extra
arguments appear as garbage. Any mismatch in the data type will
produce the unexpected result.
4. The function operates on formal arguments and sends back the
result to calling function. The return() statement performs this task.
FUNCTION DEFINITION
The following code illustrates the working of a function with its
necessary components.
int abc (int, int, int);
void main( )
{
int x, y, z;
--------------------
--------------------
abc(x,y,z); /*function call with actual arguments */
--------------------
--------------------
}
Cont..
int (int l,int k,int j) /* function definition
with formal argument*/
{
--------------------
--------------------
return(); /* return value */
}
Explanation of code in previous slides.
1. The return type of the function can be int, float, double, char,
void,etc., depending upon what is to be returned from called function to
the calling function.
By default function returns int, if return type is not mentioned.
2. Actual argument: The arguments of calling functions are actual
arguments.
variables ‘ x ’, ‘ y ’ and ‘ z ’ are actual arguments.
3. Formal argument: The arguments of the called function are formal
arguments.
Cont..
variables ‘ l ’, ‘ k ’ and ‘ j ’ are formal arguments.
4. Function name: A function name follow the same rule as we use for
naming a variable.
Example:
sum (int a, int b);
where sum() is a user-defined function. This is a function call and
‘ a ’ and ‘ b ’ are integer arguments. The function call must be ended
by a semi-colon (;).
Cont..
5. Argument/parameter list: The argument list means variable names
enclosed within the parentheses. They must be separated by a comma
(,). These formal arguments (consignment) receive values from the
actual argument for performing the communication between consignee
and consignor functions.
6. Function call: A compiler executes the function when a semi-colon
(;) is followed by the function name. A function can be called simply
using its name like other C statement, terminated by a semi-colon (;).
Write a program to show how user-defined function is called.
int add(int,int); /* function prototype */
void main ( )
{
int x=1,y=2,z;
z=add(x,y); /* FUNCTION call/
printf(“z=%d”,z);
}
/* FUNCTION DEFINITION */
int add(int a, int b)
{
return(a+b);
}
Cont…
 In the above program, values of ‘x’ and ‘y’ are passed to function
add().
 Formal arguments ‘a’ and ‘b’ receive the values.
 The function add() calculates the addition of both the variables
and returns result.
 The result is collected by the variable ‘z’ of the main() function
which is printed through the printf() statement.
Local variable
 The local variables are defined within the body of the function or
block.
 The variable defined is local to that function or block only.
 Other functions cannot access these variables.
 The compiler shows errors in case other functions try to access
the variables.
Local variable (cont..)
Example:
value(int k, int m)
{
int r,t;
}
Here ‘r’ and ‘t’ are the local variables, which are defined within the body of
the function value(). The same variable names may be defined in different
functions. They are local to that particular function.
Write a program to show how similar variable names can be used
in different functions.
void fun(void);
void main()
{
int b=10,c=5;
clrscr();
printf(“n In main() B=%d C=%d”,b,c);
fun();
}
void fun()
{
int b=20,c=10;
printf(“nIn fun() B=%d C=%d”,b,c);
}
Cont..
 In the above program, two functions are used.
 One is main() and the other user-defined function is fun().
 The variables ‘b’ and ‘c’ are defined in both the functions.
 Their effect is only within the function in which they are defined.
 Both the functions print local values of ‘b’ and ‘c’ assigned in
their functions.
 We can also declare the same variable names for actual and
formal arguments. The compiler will not get confused due to
same variable names. The scope of every variable is local to the
block in which they are defined.
Global variables:
 Global variables are defined outside the main() function.
Multiple functions can use them.
Write a program to show the effect of global variables on different
functions.
void fun(void);
int b=10,c=5;
void main()
{
clrscr();
printf(“nIn main() B=%d C=%d”,b,c);
fun();
b++;
c−−;
printf(“n Again In main() B=%d C=%d”,b,c);
}
Cont..
void fun()
{
b++;
c−−;
printf(“n In fun() B=%d C=%d”,b,c);
}
Output:
In main() B=10 C=5
In fun() B=11 C=4
Again In main() B=12 C=3
Return value
 It is the outcome of the function.
 The result obtained by the function is sent back to the calling
function through the return statement.
 The return statement returns one value per call.
 The value returned is collected by the variable of the calling
function.
THE RETURN STATEMENT
 The user-defined function uses the return statement to return
the value to the calling function.
 Exit from the called function to the calling function is done by
the use of the return statement.
 When the return statement is executed without argument, it
always return 1.
Example: /* Sending and receiving values between functions */
#include<stdio.h>
#include<conio.h>
int calsum(int,int,int);
void main( )
{
int a, b, c, sum ;
printf ( "nEnter any three numbers " ) ;
scanf ( "%d %d %d", &a, &b, &c ) ;
sum = calsum ( a, b, c );
printf ( "nSum = %d", sum ) ;
getch();
}
calsum (int x, int y, int z )
{
int d ;
d = x + y + z ;
return ( d ) ;
}
TYPES OF FUNCTIONS
 Without arguments and return values
 With arguments but without return values
 With arguments and return values
 Without arguments and but with return values
Without arguments and return values
Calling Function Analysis Called Function
void main() abc()
{ {
---------------------- ----------------------
---------------------- ----------------------
abc(); No arguments are passed ----------------------
---------------------- No values are sent back. }
----------------------
}
Without arguments and return values (cont..)
1. Data is neither passed through the calling function nor sent back
from the called function.
2. There is no data transfer between calling and called functions.
3. The function is only executed and nothing is obtained.
4. If such functions are used to perform any operation, they act
independently. They read data values and print result in the same
block.
5. Such functions may be useful to print some message, draw a line
or spilt the line.
Without arguments and return values (cont..)
Example:
#include<stdio.h>
#include<conio.h>
void message();
void main()
{
clrscr();
message();
getch();
}
void message()
{
printf("Have a nice day");
}
With arguments but without return values
Calling Function Analysis Called Function
void main() abc(y)
{ {
---------------------- ----------------------
---------------------- ----------------------
abc(x); Argument(s) are passed. ----------------------
---------------------- No values are sent back. }
----------------------
}
With arguments but without return values (cont..)
1. In this type of s functions, arguments are passed through the
calling function. The called function operates on the values. But no
result is sent back
2. Such functions are partly dependent on the calling function. The
result obtained is utilized by the called function and there is no
gain to the main().
With arguments but without return values (cont..)
Example: Write a program to pass date, month and year as parameters to a user-defined
function and display the day in the format dd/mm/yy.
#include<stdio.h>
#include<conio.h>
void dat(int ,int ,int );
void main()
{
int d,m,y;
clrscr();
printf("nEnter Date: date month and
year:");
scanf("%d %d %d",&d,&m,&y);
dat(d,m,y);
getch();
}
void dat(int x, int y, int z)
{
printf("nDate = %d/%d/%d",x,y,z);
}
Write a program to calculate square of 1 to 4 number using user
defined function.
void main()
{
int j=0;
void sqr(int);
clrscr();
for(j=1;j<5;j++)
sqr(j);
}
void sqr(int k)
{
printf("n%d", k*k);
}
Output:-
1
4
9
16
With arguments and return values
void main() abc(y)
{ {
int z; -----------
----------- y++;
z=abc(x); Argument(s) are passed. -----------
----------- -----------
----------- Values are sent back. -----------
} return(y);
}
With arguments and return values (cont..)
1. In the above example, the copy of the actual argument is passed
to the formal argument, i.e. the value of ‘x’ is assigned to ‘y’.
2. The return statement returns the incremented value of ‘y’. The
returned value is collected by ‘z’.
3. Here, data is transferred between calling and called functions, i.e.
communication between functions is made.
Write a program to send values to user-defined function and receive
and display the return value.
void main()
{
int sum(int,int,int), a,b,c,s;
clrscr();
printf(“Enter Three Numbers :”);
scanf(“%d %d %d”, &a,&b,&c);
s=sum(a,b,c);
printf(“Sum = %d”,s);
getch();
}
int sum(int x, int y, int z)
{
return(x+y+z);
}
OUTPUT:
Enter Three Numbers : 7 5 4
Sum = 16
Without arguments and but with return values
main() abc()
{ {
int z; int y=5;
----------- -----------
z=abc(); No Argument(s) are passed. -----------
----------- -----------
----------- Values are sent back. return(y);
} }
Without arguments and but with return values (cont..)
1. In the above type of function, no argument(s) is passed through
the main() function. But the called function returns values.
2. The called function is independent. It reads values from
keyboard or generates from initialization and returns the values.
3. Here, both the calling and called functions partly communicate
with each other.
Write a program to receive values from user-defined function without
passing any value through main().
void main()
{
int sum(),a,s;
clrscr();
s=sum();
printf(“Sum = %d”,s);
getch();
}
int sum()
{
int x,y,z;
printf(“n Enter Three Values :”);
scanf(“%d %d %d”,&x,&y,&z);
return(x+y+z);
}
OUTPUT:
Enter Three Values : 3 5 4
Sum = 12
Call by value
 In this type, the value of actual arguments is passed to the formal
arguments and operation is done on the formal arguments.
 Any change in the formal argument made does not affect the
actual arguments because formal arguments are the photocopy
of the actual argument.
 Hence, when a function is called by the call by value method, it
does not affect the actual contents of the arguments.
 Changes made in the formal arguments are local to the block of
the called function. Once control returns back to the calling
function, changes made vanish.
Write a program to exchange values of two variables by using ‘call by value’ to the
function.
int x,y,change (int, int);
int main()
{
clrscr();
printf(“n Enter Values of X & Y :”);
scanf(“%d %d”,&x,&y);
change(x,y);
printf(“n In main() X=%d Y=%d”,x,y);
return 0;
}
change(int a, int b)
{
int k;
k=a;
a=b;
b=k;
printf(“n In Change() X=%d Y=%d”,a,b);
}
Output:
Enter Values of X & Y : 5 4
In Change() X=4 Y=5
In main()X=5 Y=4
Cont..
 In the above program, we are passing values of actual argument
‘x’ and ‘y’ to the function change().
 The formal arguments ‘a’ and ‘b’ of function change() receive
these values().
 The values are interchanged, i.e. the value of ‘a’ is assigned to ‘b’
and vice versa and printed.
 When the control returns back to the main(), the changes made
in the function change() vanish.
 In the main(), the values of ‘x’ and ‘y’ are printed as they read
from the keyboard.
 In the call by value method, the formal argument acts as a
photocopy of the actual argument. Hence, changes made to them
are temporary.
Call by reference
 In this type, instead of passing values, addresses (reference) are
passed.
 Function operates on addresses rather than values.
 Here, the formal arguments are pointers to the actual argument.
 In this type, formal arguments point to the actual argument.
 Hence, changes made in the argument are permanent.
 The example given below illustrates the use of call by reference.
Write a program to send a value by reference to the user defined
function.
void main()
{
int x,y,change (int*, int*);
clrscr();
printf(“n Enter Values of X & Y :”);
scanf(“%d %d”,&x,&y);
change(&x,&y);
printf(“n In main() X=%d Y=%d”,x,y);
getch();
}
change(int *a, int *b)
{
int *k;
*k=*a;
*a=*b;
*b=*k;
printf(“n In Change() X=%d Y=%d”,*a,*b);
}
OUTPUT:
Enter Values of X & Y : 5 4
In Change() X=4 Y=5
In main()X=4 Y=5
Cont…
 In the above example, we are passing addresses of formal
arguments to the function change().
 The pointers in the change() receive these addresses, i.e.
pointer points to the actual argument.
 Here, the change()function operates on the actual argument
through the pointer.
 Hence, the changes made in the values are permanent.
 In this type of call, no return statement is required.
FUNCTION AS AN ARGUMENT
Example: Write a program to pass a user-defined function as an argument to
another function.
int doub(int m);
int square(int k);
void main()
{
int y=2,x;
clrscr();
x=doub(square(y));
printf(“x=%d”,x);
}
int doub(int m)
{
int p;
p=m*2;
return(p);
}
int square(int k)
{
int z;
z=k*k;
return(z);
}
OUTPUT:
x=8
Cont..
 In the above example, instead of passing a variable or its
address, a function is passed as an argument.
 The innermost function square() is executed first and
completed.
 Its return value is collected by the doub() function.
 The doub() function uses the square() function as an argument.
 It operates on the return value of square() function and makes
the returned value double and returns to the main() function.
RECURSION
 So far, we have seen function calling one another.
 In programming, there might be a situation where a function
needs to invoke itself.
 The C language supports recursive feature, i.e. a function is
called repetitively by itself.
 The recursion can be used directly or indirectly.
 The direct recursion function calls to itself till the condition is
true.
 In indirect recursion, a function calls to another function and
then called function calls to the calling function.
RECURSION (cont..)
 When a function calls itself until the last call is invoked till that
time the first call also remains open.
 At every time, a function invoked, the function returns the result
of previous call.
 The sequence of return ensues all the way up the line until the
first call returns the result to caller function.
Example: Write a program to calculate the factorial value of an integer.
void main( )
{
int a, fact ;
printf ( "nEnter any number " ) ;
scanf ( "%d", &a ) ;
fact = rec ( a ) ;
printf ( "Factorial value = %d", fact ) ;
getch();
}
rec ( int x )
{
int f ;
if ( x == 1 )
return ( 1 ) ;
else
f = x * rec ( x - 1 ) ;
return ( f ) ;
}
Cont…
 In the first run when the number entered through scanf( ) is 1, let us
see what action does rec( ) take.
 The value of a (i.e. 1) is copied into x.
 Since x turns out to be 1 the condition if ( x == 1 ) is satisfied and
hence 1 (which indeed is the value of 1 factorial) is returned through
the return statement.
 When the number entered through scanf( ) is 2, the ( x == 1 ) test
fails, so we reach the statement,
f = x * rec ( x - 1 ) ;
Cont..
 And here is where we meet recursion.
 Since the current value of x is 2, it is same as saying that we must
calculate the value (2 * rec ( 1 )).
 We know that the value returned by rec ( 1 ) is 1, so the expression
reduces to (2 * 1), or simply 2.
 Thus the statement, x * rec ( x - 1 ) ; evaluates to 2, which is stored in
the variable f, and is returned to main( ), where it is duly printed as
 Factorial value = 2
Cont…
 In case the value of a is 5, main( ) would call rec( ) with 5 as its
actual argument, and rec( ) will send back the computed value.
 But before sending the computed value, rec( ) calls rec( ) and waits
for a value to be returned.
 It is possible for the rec( ) that has just been called to call yet
another rec( ), the argument x being decreased in value by 1 for each
of these recursive calls.
 We speak of this series of calls to rec( ) as being different
invocations of rec( ).
 These successive invocations of the same function are possible
because the C compiler keeps track of which invocation calls which.
Cont…
 These recursive invocations end finally when the last
invocation gets an argument value of 1, which the preceding
invocation of rec( ) now uses to calculate its own f value and
so on up the ladder.
 So we might say what happens is,
 rec ( 5 ) returns ( 5 times rec ( 4 ),
which returns ( 4 times rec ( 3 ),
which returns ( 3 times rec ( 2 ),
which returns ( 2 times rec ( 1 ),
which returns ( 1 ) ) ) ) )
Advantages of Recursion
1. Although, at most of the times, a problem can be solved without
recursion, but in some situations in programming, it is a must to
use recursion. For example, a program to display a list of all files
of the system cannot be solved without recursion.
2. The recursion is very flexible in data structure like stacks,
queues, linked list and quick sort.
3. Using recursion, the length of the program can be reduced.
Disadvantages of Recursion
1. It requires extra storage space. The recursive calls and automatic
variables are stored on the stack. For every recursive calls, separate
memory is allocated to automatic variables with the same name.
2. If the programmer forgets to specify the exit condition in the
recursive function, the program will execute out of memory. In such
a situation user has to press Ctrl+ break to pause and stop the
function.
3. The recursion function is not efficient in execution speed and
time.

More Related Content

Similar to Unit-III.pptx (20)

[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
C functions list
C functions listC functions list
C functions list
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 

Recently uploaded

Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 

Recently uploaded (20)

Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 

Unit-III.pptx

  • 2. INTRODUCTION  It is difficult to prepare and maintain a large-sized program.  Moreover, the identification of the flow of data is hard to understand.  The best way to prepare a programming application is to divide the bigger program into small pieces or modules and the same modules can be repeatedly called from the main() function as and when required.
  • 3. INTRODUCTION (CONT..)  These small modules or subprograms are easily manageable.  This method is called the divide and conquer method.  In C, such small modules are called functions.  The programs written in the C language are highly dependent on functions.  The C program is nothing but a combination of one or more functions.
  • 4. INTRODUCTION (CONT..)  A program divided in multiple functions
  • 5. INTRODUCTION (CONT..)  Every C program starts with the user-defined function main().  Each time when a new program is started, main() function must be defined.  The main() function calls another functions to share the work.  The main program can supply data to a function.  A specific operation is performed on the data and a value is returned to the calling function.
  • 6. INTRODUCTION (CONT..)  The C language supports two types of functions:  (1) library functions and  (2) user-defined functions.
  • 7. INTRODUCTION (CONT..) (1) library functions :  The library functions are pre-defined set of functions. Their task is limited.  One can only use the functions but cannot change or modify them. For example, sqrt (81) gives result 9.  The user do not know the internal working of function
  • 8. INTRODUCTION (CONT..) (2) user-defined functions:  The functions defined by the user according to his/her requirement are called user-defined functions.  A user can modify the functions according to the requirement.  A user certainly understands the internal working of the function.  The user has full scope to implement his/her own ideas in the function.  Thus, the set of such user-defined functions can be useful to another programmer.
  • 9. INTRODUCTION (CONT..)  One should include the file in which the user-defined functions are stored to call the function in the program.  For example, let square(9) be a user-defined function that gives the result 81.  Here, the user knows the internal working of the square() function, as its source code is visible.  This is the major difference between the two types of functions.
  • 10. BASICS OF A FUNCTION  A function is a self-contained block or a sub-program of one or more statements that perform a special task when called.  Why Use Functions? 1. If we want to perform a task repetitively, then it is not necessary to re-write the particular block of the program again and again. Shift the particular block of statements in a user-defined function. The function defined can be called any number of times to perform the task. 2. Using functions, large programs can be reduced to smaller ones. It is easy to debug and find out the errors in it. It also increases readability.
  • 11. BASICS OF A FUNCTION (CONT..)  How a Function Works? 1. Once a function is defined and called, it takes some data from the calling function and returns a value to the called function. 2. The detail of inner working of a function is unknown to the rest of the program. Whenever a function is called, control passes to the called function and working of calling function is paused. When the execution of a called function is completed, control returns back to the calling function and executes the next statement.
  • 12. BASICS OF A FUNCTION (CONT..)  How a Function Works? (cont..) 3. The values of actual arguments passed by the calling function are received by the formal arguments of the called function. The number of actual and formal arguments should be the same. Extra arguments are discarded if they are defined. If the formal arguments are more than the actual arguments, then the extra arguments appear as garbage. Any mismatch in the data type will produce the unexpected result. 4. The function operates on formal arguments and sends back the result to calling function. The return() statement performs this task.
  • 14. The following code illustrates the working of a function with its necessary components. int abc (int, int, int); void main( ) { int x, y, z; -------------------- -------------------- abc(x,y,z); /*function call with actual arguments */ -------------------- -------------------- }
  • 15. Cont.. int (int l,int k,int j) /* function definition with formal argument*/ { -------------------- -------------------- return(); /* return value */ }
  • 16. Explanation of code in previous slides. 1. The return type of the function can be int, float, double, char, void,etc., depending upon what is to be returned from called function to the calling function. By default function returns int, if return type is not mentioned. 2. Actual argument: The arguments of calling functions are actual arguments. variables ‘ x ’, ‘ y ’ and ‘ z ’ are actual arguments. 3. Formal argument: The arguments of the called function are formal arguments.
  • 17. Cont.. variables ‘ l ’, ‘ k ’ and ‘ j ’ are formal arguments. 4. Function name: A function name follow the same rule as we use for naming a variable. Example: sum (int a, int b); where sum() is a user-defined function. This is a function call and ‘ a ’ and ‘ b ’ are integer arguments. The function call must be ended by a semi-colon (;).
  • 18. Cont.. 5. Argument/parameter list: The argument list means variable names enclosed within the parentheses. They must be separated by a comma (,). These formal arguments (consignment) receive values from the actual argument for performing the communication between consignee and consignor functions. 6. Function call: A compiler executes the function when a semi-colon (;) is followed by the function name. A function can be called simply using its name like other C statement, terminated by a semi-colon (;).
  • 19. Write a program to show how user-defined function is called. int add(int,int); /* function prototype */ void main ( ) { int x=1,y=2,z; z=add(x,y); /* FUNCTION call/ printf(“z=%d”,z); } /* FUNCTION DEFINITION */ int add(int a, int b) { return(a+b); }
  • 20. Cont…  In the above program, values of ‘x’ and ‘y’ are passed to function add().  Formal arguments ‘a’ and ‘b’ receive the values.  The function add() calculates the addition of both the variables and returns result.  The result is collected by the variable ‘z’ of the main() function which is printed through the printf() statement.
  • 21. Local variable  The local variables are defined within the body of the function or block.  The variable defined is local to that function or block only.  Other functions cannot access these variables.  The compiler shows errors in case other functions try to access the variables.
  • 22. Local variable (cont..) Example: value(int k, int m) { int r,t; } Here ‘r’ and ‘t’ are the local variables, which are defined within the body of the function value(). The same variable names may be defined in different functions. They are local to that particular function.
  • 23. Write a program to show how similar variable names can be used in different functions. void fun(void); void main() { int b=10,c=5; clrscr(); printf(“n In main() B=%d C=%d”,b,c); fun(); } void fun() { int b=20,c=10; printf(“nIn fun() B=%d C=%d”,b,c); }
  • 24. Cont..  In the above program, two functions are used.  One is main() and the other user-defined function is fun().  The variables ‘b’ and ‘c’ are defined in both the functions.  Their effect is only within the function in which they are defined.  Both the functions print local values of ‘b’ and ‘c’ assigned in their functions.  We can also declare the same variable names for actual and formal arguments. The compiler will not get confused due to same variable names. The scope of every variable is local to the block in which they are defined.
  • 25. Global variables:  Global variables are defined outside the main() function. Multiple functions can use them.
  • 26. Write a program to show the effect of global variables on different functions. void fun(void); int b=10,c=5; void main() { clrscr(); printf(“nIn main() B=%d C=%d”,b,c); fun(); b++; c−−; printf(“n Again In main() B=%d C=%d”,b,c); }
  • 27. Cont.. void fun() { b++; c−−; printf(“n In fun() B=%d C=%d”,b,c); } Output: In main() B=10 C=5 In fun() B=11 C=4 Again In main() B=12 C=3
  • 28. Return value  It is the outcome of the function.  The result obtained by the function is sent back to the calling function through the return statement.  The return statement returns one value per call.  The value returned is collected by the variable of the calling function.
  • 29. THE RETURN STATEMENT  The user-defined function uses the return statement to return the value to the calling function.  Exit from the called function to the calling function is done by the use of the return statement.  When the return statement is executed without argument, it always return 1.
  • 30. Example: /* Sending and receiving values between functions */ #include<stdio.h> #include<conio.h> int calsum(int,int,int); void main( ) { int a, b, c, sum ; printf ( "nEnter any three numbers " ) ; scanf ( "%d %d %d", &a, &b, &c ) ; sum = calsum ( a, b, c ); printf ( "nSum = %d", sum ) ; getch(); } calsum (int x, int y, int z ) { int d ; d = x + y + z ; return ( d ) ; }
  • 31. TYPES OF FUNCTIONS  Without arguments and return values  With arguments but without return values  With arguments and return values  Without arguments and but with return values
  • 32. Without arguments and return values Calling Function Analysis Called Function void main() abc() { { ---------------------- ---------------------- ---------------------- ---------------------- abc(); No arguments are passed ---------------------- ---------------------- No values are sent back. } ---------------------- }
  • 33. Without arguments and return values (cont..) 1. Data is neither passed through the calling function nor sent back from the called function. 2. There is no data transfer between calling and called functions. 3. The function is only executed and nothing is obtained. 4. If such functions are used to perform any operation, they act independently. They read data values and print result in the same block. 5. Such functions may be useful to print some message, draw a line or spilt the line.
  • 34. Without arguments and return values (cont..) Example: #include<stdio.h> #include<conio.h> void message(); void main() { clrscr(); message(); getch(); } void message() { printf("Have a nice day"); }
  • 35. With arguments but without return values Calling Function Analysis Called Function void main() abc(y) { { ---------------------- ---------------------- ---------------------- ---------------------- abc(x); Argument(s) are passed. ---------------------- ---------------------- No values are sent back. } ---------------------- }
  • 36. With arguments but without return values (cont..) 1. In this type of s functions, arguments are passed through the calling function. The called function operates on the values. But no result is sent back 2. Such functions are partly dependent on the calling function. The result obtained is utilized by the called function and there is no gain to the main().
  • 37. With arguments but without return values (cont..) Example: Write a program to pass date, month and year as parameters to a user-defined function and display the day in the format dd/mm/yy. #include<stdio.h> #include<conio.h> void dat(int ,int ,int ); void main() { int d,m,y; clrscr(); printf("nEnter Date: date month and year:"); scanf("%d %d %d",&d,&m,&y); dat(d,m,y); getch(); } void dat(int x, int y, int z) { printf("nDate = %d/%d/%d",x,y,z); }
  • 38. Write a program to calculate square of 1 to 4 number using user defined function. void main() { int j=0; void sqr(int); clrscr(); for(j=1;j<5;j++) sqr(j); } void sqr(int k) { printf("n%d", k*k); } Output:- 1 4 9 16
  • 39. With arguments and return values void main() abc(y) { { int z; ----------- ----------- y++; z=abc(x); Argument(s) are passed. ----------- ----------- ----------- ----------- Values are sent back. ----------- } return(y); }
  • 40. With arguments and return values (cont..) 1. In the above example, the copy of the actual argument is passed to the formal argument, i.e. the value of ‘x’ is assigned to ‘y’. 2. The return statement returns the incremented value of ‘y’. The returned value is collected by ‘z’. 3. Here, data is transferred between calling and called functions, i.e. communication between functions is made.
  • 41. Write a program to send values to user-defined function and receive and display the return value. void main() { int sum(int,int,int), a,b,c,s; clrscr(); printf(“Enter Three Numbers :”); scanf(“%d %d %d”, &a,&b,&c); s=sum(a,b,c); printf(“Sum = %d”,s); getch(); } int sum(int x, int y, int z) { return(x+y+z); } OUTPUT: Enter Three Numbers : 7 5 4 Sum = 16
  • 42. Without arguments and but with return values main() abc() { { int z; int y=5; ----------- ----------- z=abc(); No Argument(s) are passed. ----------- ----------- ----------- ----------- Values are sent back. return(y); } }
  • 43. Without arguments and but with return values (cont..) 1. In the above type of function, no argument(s) is passed through the main() function. But the called function returns values. 2. The called function is independent. It reads values from keyboard or generates from initialization and returns the values. 3. Here, both the calling and called functions partly communicate with each other.
  • 44. Write a program to receive values from user-defined function without passing any value through main(). void main() { int sum(),a,s; clrscr(); s=sum(); printf(“Sum = %d”,s); getch(); } int sum() { int x,y,z; printf(“n Enter Three Values :”); scanf(“%d %d %d”,&x,&y,&z); return(x+y+z); } OUTPUT: Enter Three Values : 3 5 4 Sum = 12
  • 45. Call by value  In this type, the value of actual arguments is passed to the formal arguments and operation is done on the formal arguments.  Any change in the formal argument made does not affect the actual arguments because formal arguments are the photocopy of the actual argument.  Hence, when a function is called by the call by value method, it does not affect the actual contents of the arguments.  Changes made in the formal arguments are local to the block of the called function. Once control returns back to the calling function, changes made vanish.
  • 46. Write a program to exchange values of two variables by using ‘call by value’ to the function. int x,y,change (int, int); int main() { clrscr(); printf(“n Enter Values of X & Y :”); scanf(“%d %d”,&x,&y); change(x,y); printf(“n In main() X=%d Y=%d”,x,y); return 0; } change(int a, int b) { int k; k=a; a=b; b=k; printf(“n In Change() X=%d Y=%d”,a,b); } Output: Enter Values of X & Y : 5 4 In Change() X=4 Y=5 In main()X=5 Y=4
  • 47. Cont..  In the above program, we are passing values of actual argument ‘x’ and ‘y’ to the function change().  The formal arguments ‘a’ and ‘b’ of function change() receive these values().  The values are interchanged, i.e. the value of ‘a’ is assigned to ‘b’ and vice versa and printed.  When the control returns back to the main(), the changes made in the function change() vanish.  In the main(), the values of ‘x’ and ‘y’ are printed as they read from the keyboard.  In the call by value method, the formal argument acts as a photocopy of the actual argument. Hence, changes made to them are temporary.
  • 48. Call by reference  In this type, instead of passing values, addresses (reference) are passed.  Function operates on addresses rather than values.  Here, the formal arguments are pointers to the actual argument.  In this type, formal arguments point to the actual argument.  Hence, changes made in the argument are permanent.  The example given below illustrates the use of call by reference.
  • 49. Write a program to send a value by reference to the user defined function. void main() { int x,y,change (int*, int*); clrscr(); printf(“n Enter Values of X & Y :”); scanf(“%d %d”,&x,&y); change(&x,&y); printf(“n In main() X=%d Y=%d”,x,y); getch(); } change(int *a, int *b) { int *k; *k=*a; *a=*b; *b=*k; printf(“n In Change() X=%d Y=%d”,*a,*b); } OUTPUT: Enter Values of X & Y : 5 4 In Change() X=4 Y=5 In main()X=4 Y=5
  • 50. Cont…  In the above example, we are passing addresses of formal arguments to the function change().  The pointers in the change() receive these addresses, i.e. pointer points to the actual argument.  Here, the change()function operates on the actual argument through the pointer.  Hence, the changes made in the values are permanent.  In this type of call, no return statement is required.
  • 51. FUNCTION AS AN ARGUMENT Example: Write a program to pass a user-defined function as an argument to another function. int doub(int m); int square(int k); void main() { int y=2,x; clrscr(); x=doub(square(y)); printf(“x=%d”,x); } int doub(int m) { int p; p=m*2; return(p); } int square(int k) { int z; z=k*k; return(z); } OUTPUT: x=8
  • 52. Cont..  In the above example, instead of passing a variable or its address, a function is passed as an argument.  The innermost function square() is executed first and completed.  Its return value is collected by the doub() function.  The doub() function uses the square() function as an argument.  It operates on the return value of square() function and makes the returned value double and returns to the main() function.
  • 53. RECURSION  So far, we have seen function calling one another.  In programming, there might be a situation where a function needs to invoke itself.  The C language supports recursive feature, i.e. a function is called repetitively by itself.  The recursion can be used directly or indirectly.  The direct recursion function calls to itself till the condition is true.  In indirect recursion, a function calls to another function and then called function calls to the calling function.
  • 54. RECURSION (cont..)  When a function calls itself until the last call is invoked till that time the first call also remains open.  At every time, a function invoked, the function returns the result of previous call.  The sequence of return ensues all the way up the line until the first call returns the result to caller function.
  • 55. Example: Write a program to calculate the factorial value of an integer. void main( ) { int a, fact ; printf ( "nEnter any number " ) ; scanf ( "%d", &a ) ; fact = rec ( a ) ; printf ( "Factorial value = %d", fact ) ; getch(); } rec ( int x ) { int f ; if ( x == 1 ) return ( 1 ) ; else f = x * rec ( x - 1 ) ; return ( f ) ; }
  • 56. Cont…  In the first run when the number entered through scanf( ) is 1, let us see what action does rec( ) take.  The value of a (i.e. 1) is copied into x.  Since x turns out to be 1 the condition if ( x == 1 ) is satisfied and hence 1 (which indeed is the value of 1 factorial) is returned through the return statement.  When the number entered through scanf( ) is 2, the ( x == 1 ) test fails, so we reach the statement, f = x * rec ( x - 1 ) ;
  • 57. Cont..  And here is where we meet recursion.  Since the current value of x is 2, it is same as saying that we must calculate the value (2 * rec ( 1 )).  We know that the value returned by rec ( 1 ) is 1, so the expression reduces to (2 * 1), or simply 2.  Thus the statement, x * rec ( x - 1 ) ; evaluates to 2, which is stored in the variable f, and is returned to main( ), where it is duly printed as  Factorial value = 2
  • 58. Cont…  In case the value of a is 5, main( ) would call rec( ) with 5 as its actual argument, and rec( ) will send back the computed value.  But before sending the computed value, rec( ) calls rec( ) and waits for a value to be returned.  It is possible for the rec( ) that has just been called to call yet another rec( ), the argument x being decreased in value by 1 for each of these recursive calls.  We speak of this series of calls to rec( ) as being different invocations of rec( ).  These successive invocations of the same function are possible because the C compiler keeps track of which invocation calls which.
  • 59. Cont…  These recursive invocations end finally when the last invocation gets an argument value of 1, which the preceding invocation of rec( ) now uses to calculate its own f value and so on up the ladder.  So we might say what happens is,  rec ( 5 ) returns ( 5 times rec ( 4 ), which returns ( 4 times rec ( 3 ), which returns ( 3 times rec ( 2 ), which returns ( 2 times rec ( 1 ), which returns ( 1 ) ) ) ) )
  • 60. Advantages of Recursion 1. Although, at most of the times, a problem can be solved without recursion, but in some situations in programming, it is a must to use recursion. For example, a program to display a list of all files of the system cannot be solved without recursion. 2. The recursion is very flexible in data structure like stacks, queues, linked list and quick sort. 3. Using recursion, the length of the program can be reduced.
  • 61. Disadvantages of Recursion 1. It requires extra storage space. The recursive calls and automatic variables are stored on the stack. For every recursive calls, separate memory is allocated to automatic variables with the same name. 2. If the programmer forgets to specify the exit condition in the recursive function, the program will execute out of memory. In such a situation user has to press Ctrl+ break to pause and stop the function. 3. The recursion function is not efficient in execution speed and time.