Dca1102-Programming in C - de Unit 5
Dca1102-Programming in C - de Unit 5
1_Functions in C
Hello and welcome to the session. C programming is a modular programming language which allows
the programmer to follow a divide and conquer approach, wherein a bigger task is divided into
several smaller sub tasks, which performs a specific action. Now these tasks or sub tasks or modules
are called as functions in C, which are self-contained program segments that carry out some specific,
well defined tasks. In this session, we would learn or get to know the importance of functions that
are used, that can be used in C programming and also differentiate between the function declaration
and function definition and how to operate data with formal arguments and actual arguments,
which helps to communicate between different functions.
Every C program must have at least one function, and every C program must contain a main
function. Because main function is where the execution of the program begins. Now, every function
name can be given a unique name and every function name has to be a unique name. Now this
name is used to call function from main function or any function can call another function. A
function is independent and it can perform its task without any interference or intervention from
the other parts of the program.
Now, as we understand that one function can call another function. We have calling functions and
called functions. A function, which makes a call to another function, is called as a calling function. A
function, which is being called, is called as a called function. For example, let us say we have a main
function which calls an add function for arithmetic addition. So, when a function, main function
places a call to add function, then main function is called as a calling function and add function is
called as a called function. Now having said this, your calling function can place or pass certain data
to called function in the form of arguments or parameters and also this called function can respond
back to the calling function, with the return value by using a return statement or it may choose not
to respond back, in which case we use void return type. So, this is a program where we can see
which function acts as a calling function and which function acts as a called function. Now in this
program, if we see the first statement which is a header file, after which there is a main function. As
said, every C program must have a main function where the execution of the program begins. Inside
the main function the first statement is a declaration statement where A, B, some variables are
declared to be integers.
Now, in the next few statements, we have the user. We are asking the user to enter the value for A
and B and both values A and B will be read through the scanner statement and the values will be
placed in the address locations of A and B through the address operators that we have used along
with the variables here. And next we have a statement, Sum is equal to add A comma B, where a
function call for add function is being made by sending A and B values as its parameters or as its
arguments. Now this is a calling function, that is, add function is a called function and main function
here is the calling function. Now, as soon as the compiler sees this statement, a function is invoked.
After the function call is being made, the function is invoked and the control will be simply
transferred to where the function has been defined. So here is where in this section we have
function that has been defined, so the control will simply jump to this part of program. Now here in
this function definition, we are having certain statements and also this function definition has it's
parameters A and B, where the values will be passed from the calling function to the called function.
Now the values are the values that the user has entered and are stored in A and B locations. Here in
this case, it would be 10 and 20. Now this 10 and 20 will be passed on to the add function. Here add
function will add these two variables A plus B, that is value 10 and 20 will be added and will be
stored in C and this C will be returned to the calling function where the function call has been made.
Now after it returns the statement, that means now add A comma B, function will be returning the
value C, that is after addition it would be 30, so the value 30 will be placed or stored in the variable
sum here. Now after this, the control will be passed on to the next statement after the Function call,
which is print F statement. Here the statement that is displayed is the resultant sum of two values is
percentage D sum. Now percentage D being a format specifier, the control or the compiler sees the
argument that is attached with this format specifier. The argument that we have here is sum. Sum is
nothing but the value that has been returned by the function and has been stored in its place. So, 30
will be displayed on to the console. So, on the whole, as a resultant, or as an output we have the
resultant sum of two values is 30.
So, functions help in programming by allowing modularity where divide and conquer approach is
followed. So basically, this divide and conquer approach is to divide the whole problem or a bigger
problem into smaller segments of code. And also we can declare variables inside this function, and
these variables will be local to the function and will only be known to the function. That means to
say that the control when it comes out of the function, the variable, when the control comes out of
the function, the variables, which are local to the function will not have any life or will be inactive,
meaning the scope of the variables will be limited only to the function and not more than that. Also,
the data that is sent by the called function to calling function for Communication will also be
considered as local variables to the function. The main benefit of having functions in C programming
language is to follow a divide and conquer approach which manages the program development and
also to construct a program from smaller pieces or components. And also the other benefit is to
reuse the functions, that is, we use the existing functions as building blocks for new programs and
also we can use many inbuilt functions that C program provides in the form of header files, which
also follows a technique called as abstraction, by hiding its implementation details.
So, every function has to be defined before we use it. So, to define a function, it has two parts. One
is function header and function body. So, the whole function definition will tell the compiler what
kind of function that we're using and what kind of data that a function is going to use. So, the syntax
of the function definition will have a function header with its return type function and list of
arguments and a body of function will contain the executable statements where the logic of function
would actually be written. This is the broader view of function Header. So, the return type here is
nothing but the type of value that the function would return. The type of value can be an integer,
can be a float value, can be a constant value and function here is the function name or the name of
the function which you want to give. Now functions can either be library functions that you'll be
using, wherein you don't have to name the function or a user defined function where you have to
give a valid function name. So we have to keep in mind that we should not be using any keyword
names, so we should give a valid name to the name, as a function name here, and also this function
would be followed by several arguments which are called as formal arguments. Each argument
should be declared along with its data type here.
So here is the broader view of function body, which comes along with the function header. Now,
after the function header, the body of the function is included between an open brace and closed
brace.
So, the execution of function definition begins from the open brace here and ends after the; and
ends at the closing brace of the function. Now the body of the function might have any declaration,
statements or any statements under Return Value. Here is a sample program where we can see how
a function call is being made and how arguments are being passed between the calling function and
called function. What I mean by arguments is, that the data that can be passed from calling function
to the called function. So firstly, we have a main function where in two variables, Var1 and Var2 will
be declared. Now here variable 1 and variable 2 are initialized with values from the user input, after
which, in a print F statement, a function call is being made. The name of the function is average, so
in which we're passing two variables data, which is Var1 and Var2. Now these arguments which we
are passing in the calling function, to the function call, are called as actual arguments. So, we have to
remember that the calling function, while placing a function call will send actual arguments to the
function definition.
As soon as the compiler sees the function call, it transfers the control to the function definition. Here
is the function definition with the function header, where the int is the return type of the function,
average is the name of the function, and we have a set of parameters which are called as formal
parameters. So, any parameters which are in function definition are called as formal arguments or
formal parameters. Now the difference between the parameter and an argument is that parameter
is the one which are at the entry level of the function definition. Arguments are the values that are
actually actual values that are sent to the function. There are a few things that have to be noted
while defining a function. Any function or a function cannot be defined inside the function, and also
all the functions should be dis-joint, meaning every function should work independently and should
not be dependent on other parts of the program or any other functions.
So now here in this program, we will see what happens if the function is defined inside the function.
Here we have a main function which is used as a calling function, and this main function is placing a
call to a function called as average, in a print F statement. This is where a function call has been
made. And immediately after the place in the function call we are trying to define this particular
function, inside the main function itself.
So, the compiler places an error. You can see the highlighted area where the errors are being
displayed by the compiler. These are the errors that would be displayed by the compiler. Now it says
that at any point of time, a function can never be defined inside the function, you can declare a
function before the function. You can call a function or place a function call, but a function should
never be defined inside another function.
Also, a function can have nested calls that means function A can be calling function B and function
be can be calling functions C. Also, at times, in some situations a function might have to call itself so,
a technique where a function calls itself is called as recursion or the functions, such functions will be
called as recursive functions. So here is a program where we would see how the nested function
calls work. Now, firstly, we have main function, inside which we are declaring two variables, variable
1 and variable 2 whose values are read from the user. And then those values will be allocated or sent
to the address locations of variable 1 and variable 2. Then in the next statement, in the printer
statement, we are displaying a statement saying the average value of two values is percentage D and
we are placing a function call here. That means this format specifier where will take its argument as
a function call.
Now, as soon as the compiler sees it, the function call, the control will be transferred to the function
definition where average is, avg is defined and also this main function is sending two variables
through the function call to the function definition. The variables that are sent here are variable 1
and variable 2 which are already declared inside the main function. So now the control will be
transferred to the function definition where avg is defined. Here, this function has two parameters,
One is num 1 another is num 2. But if you observe, while sending the data from the calling function,
which is main function, we have sent variable 1 and variable 2. But here the values or the variable
names are num 1 and num 2. Now it is possible to have different variable names, informal
parameters and actual parameters. But whatever the values that are sent by the calling function will
be copied to the called function parameters. That means variable 1 value will be copied to num 1
and variable 2 value will be copied to num 2. Now, inside the function definition, we have several
display statements. All these display statements are just to interact with the user, after which we are
placing another function call inside avg function, which is a function called 2AV. Now here we are
sending two parameters as num 1 and num 2, which the function definition will take.
Now as soon as the function call is being made here, the control will be transferred to the function
definition here. Now, inside this as control is transferred to AV function now. Now AV function has a
return type of int and also has two variables which are declared inside the parameter section which
are A and B. That means to say that now these are the formal parameters that we have. Now num 1
will pass its value to A and num 2to will pass its value to B. Suppose I have entered 10 and 20 onto
the console, variable 1 and variable 2 will be taking that 10 and 20, will be passing it to the average
function avg here in num 1 and num 2 as 10 and 20 and avg will be sending that 10, 20 in the form of
num 1 one and num 2 to AV function which will now be holding 10 in the variable A and 20 in the
variable B. Now, after which the control will enter the function definition. Here we have certain
statements which are being displayed, and also there is a declaration of a variable C. Now here,
there is a statement here where A plus B is calculated and is divided by two and result will be
captured or stored in the value or variable C, and that variable is returned from this function. Now
the control will take this value of C through return statement and will be getting back to the function
where the function call was placed. So it gets back to the calling function of AV.
The calling function of AV is avg, so it gets back to this section. Now avg will take the return value or
will take the value of C and sends it to the main function where the function call has been placed. So
here you can see in the output how the values are entered and how the values are passed from one
function to the other function. Firstly, the main function will display a statement saying, enter two
values onto the console where you will be entering two values 10 and 20 which are stored in
variable 1 and variable 2 which are passed on to avg function. Control is now in avg function where
the display statements inside avg function. Hi, I'm, a function by name average, are all displayed and
after which the function called to AV is made and control will be transferred to AV function inside
which we have other displaced statements like AV function, Thank you for coming to me etcetera.
And then the AV function calculates the value of C and the C value will be returned. Now C value will
be returned to avg function. Avg function will in turn return the value to main function where the
display statement which says the average value of two values is displayed, along with the former
specifier percentage D to which the argument will be the function call for avg function which is
made. So as avg function returns value 15 that is being displayed here.
So, this is nothing but the value C that is calculated in AV which is sent to avg. In turn avg sending to
main function. Declaring functions before they are defined has to be an important practice that a
programmer has to make. Now function declaration is also called as function prototyping. The
reason why it is very important is, it actually tells the compiler, what is the name of the function that
we will be defining? What return value or what is the return type of that particular function? How
many number of arguments or the type of arguments that the function will be using while defining a
function? Here is where we are declaring a function in this program. Int avg(int, float) is nothing but
the function prototype or the function declaration, which is done before the main function. So, this
statement tells the compiler about the function name, about the function return type and also the
kind of values and number of values avg function would be using. Now this also has to be, or every
function prototype statement or function declaration statement has to be ended with semi colon, so
as to stop or make the difference between the function prototype and function definition.
So here, once we do that, the compiler will understand that later at some point of time or at some
section of the program you will be defining a function by name, avg, which will return, which has a
return type integer and will be using one int data on one float data. Now after this, in the main
function, we would place a function call, and the control after seeing the function call will directly;
compiler after seeing the function called will directly jump onto the function definition here. Here,
the return type of the function definition or the function header should be matched with the
declared functions return type and both names of function prototype and function definition should
be matched. And also, the type of the data that we are using should be matched with the type of the
arguments that we are using here in the function definition. And also the number and the order
should be matched. In case of using user defined functions it is; we use function prototyping by
declaring the function. But in case if we have to use library file; library functions, then we make use
of header files by simply including the header file with the help of hash include statement. Now this
will contain all the function prototypes of library functions, for example, here, hash include math.h
will contain all the function prototypes of math functions. And also it is possible that a user can
define his own functions and add them to the library, which he can make use of later in the program.
But if he's using it, then he has to include that particular header file. And if he has to do that, then he
has to mention the path of the header file by using the hash include statement; by using hash
include keyword, but the file name has to be included within the double quotes. Parameter passing
is a technique where we invoke the functions. Now there should be a communication between the
calling function and called function, and this calling function can send some data in the form of
arguments to the called function. Now, this can be done by using two methods. One is call by value
method and another is call by reference method. Now in call by value method it doesn't affect the
content of actual arguments, and also the copy of the actual arguments will be passed on to the
formal arguments. As said, parameters and arguments are usually used as synonyms. But
parameters are at the entry level where we are defining the function, and arguments are the actual
value which will be used by the function.
But we can use both of them as synonyms. And any functions that are made in the called functions
to the formal arguments will not be visible or will not be affecting the calling function to its actual
arguments. Now in call by reference method, instead of sending variables directly, we would send
the address locations of the variables where we can manipulate the data directly. Now this address
or reference will be passed to the function by the calling function. Now, this function operates on
the address of the variable rather than the value. And here the formal arguments will be calling the
actual arguments. Here is a program where the data or parameters will be passed through call by
value method. Now here, we have declared the function. The function that is declared is swap. The
name of the function is swap. The return type is void. Then we are using two integer values here as
the data, in the function definition. And inside the main function, a function call will be made to
swap function. Here is the function call that is being made. Now the function will be invoked. Now
the values that are sent as parameters through swap function are the values that are declared inside
the main function, which is a calling function and also have been initialized by taking input from the
user.
Now, as soon as the function call is made and the function is invoked, the control will be passed on
to the function definition here. Here is where the function is defined. Now, swap is the same
function name that we have declared and void is the same return type that we have used in the
declaration and we have passed; we are using two integer values here which are matching with the
function declaration here. Now, the values that are sent through the calling function are A and B,
and the variables that I'm using here in the function definition are X and Y. So simply the values of A
and B will be copied to X and Y, respectively. That means the value of A will be copied into the
variable X and the value of B will be copied into the variable Y. Now this function, swap function has
a temporary variable. It does the swapping functionality or swapping operation, after which it is
using a display statement saying the values after swapping are X is equal to percentage D and Y is
equal to percentage D. The first format specifier takes the first argument and the second format
specifier takes the second argument. The value of X will be returned to the first format specifier. The
value of Y will be returned to the second format specifier here. Thus onto the console we can see the
values after swapping are, will be displayed as X is equal to 20 and Y is equal to 10. That means
values after manipulation will be displayed as 20 and 10. Now after this, the control will be switched
back or transferred back to the statement after the function call. So here is where the control would
come to. This is the printF statement where the values after the change are being displayed. So, we
would see whether the values have been changed or not here. Here, the values after the change are
A is equal to 10 and B is equal to 20. That means to say that though the formal argument; the data of
formal arguments have been changed in the called function, it has not affected the calling function
values. So here is the other program to demonstrate how call by reference method works. Now in
this program, we would be; Instead of sending the variables, we will be sending the address
locations of the variables. Also, we have to note that here we would be using pointer variables,
which would be holding the address location of the variables. So here in the declaration statement,
function declaration, we have void swap int pointer and pointer. In int pointer, comma int pointer.
So swap is the function name and void would be the return type of a swap. And here int star and star
says that this swab function will be using pointer variables so instead of normal variables, it would
be using pointer variables. The reason being, we will be sending address locations, not just the
values of the variables. So here is the function call that is being made where we'll be sending swap
amp % A and amp % B. Amp percent is nothing but the address operator that is attached with the
variable. Now here we are sending the address locations inside the swap function. Now, the
variables values will be taken by the user through the user input. But here, A and B address locations
will be sent as data. Now, after which the control will be passed on to the swap function definition
here. Now this function definition has a two arguments, which are; where it is using two pointer
variables to hold the address location. So, address location of A will be sent to the pointer variable X
and address location of B will be sent to the pointer variable Y. Once this is done, then the swapping
function is done with the help of pointer variables here and after which the values after swapping
are, X is equal to percentage D and Y equal to percentage D will be printed onto the console. The
first pointer variable will be returning the value to the first format specifier and the second pointer
Variable will be returning the value to the second format specifier. Now Pointer X will give the value
that is present in the address location, and Y will also be giving the value that is there in the address
location. So indirectly the address locations are the locations of A and B here. Now in the; onto the
output console the values after swapping are, X is equal to 20 and Y equal to 10 will be displayed,
after which the function control will be returned to the next statement after the function call. Now
here, the next statement that would be displayed onto the console are, the values even after the
change are A equal to 10 and B equal to 20. So this is how call by reference method can be used to
send the data from calling function to the called function. Thank you as we have reached the end of
the session.
Programming in C_5.2_Recursion
Hello and welcome to the session. Functions can have nested calls where one function can call
another function like, say, for example, function A can call function B and function B can call function
C. Or sometimes a function has to call itself. A process where a function calls itself is nothing but a
recursion. Now, in this session, we would see how to apply recursion technique, how to use a
recursive functions effectively and also how to implement them in the program. And we will also see
different types of recursions that can be used. So the basic idea to have recursion technique is that
to find solution to smaller problems rather than to find solution to bigger problem once for all. Now,
when we decompose a bigger problem into smaller problem, it becomes very easy for us to find a
solution to the smaller sub problems. And after finding the solution to these smallest sub-problems,
we backtrack all the solutions to get a single and final solution. In recursion, where the function call
itself, calling a copy of itself and working on a smaller problem will solve a bigger problem. That is
one thing. And also during the process, one recursive, recursion step may cause several such
recursive calls and while making these recursive calls, it should not be unending or it should not get
into an infinite number of calls. So we must have a base case to terminate this recursion.
That means to say that for example, we have a recursive function ad which is calling itself. Now
when it calls itself, it should not run into an infinite loop. Rather, it should have a base case where it
terminates the recursion. We have two kinds of recursion, one is direct recursion and another is
indirect recursion. Now direct recursion is, function would call itself inside the function and Indirect
function is, function will call function two and function two will call back function one. So, such
recursion is called as indirect recursion. So this is how direct reduction will look like. Now, fact is a
function that is defined here. Now inside the function we have another; inside the fact function
we're making a function call to itself. So we can see that fact is calling itself again and again. Now
such kind of functions are called as direct recursion functions which calls repeatedly, until certain
conditions is met. This example shows indirect recursion where one function calls another function,
and this function will eventually call the same function to be called. So here, fun, is a function which
will be calling function 1 by name, fun1. And when the control jumps to function 1, fun1, then inside
this function definition, a call is being placed to the same function eventually, which is fun1.
So this is one kind of recursion that we have. So here is how recursive, recursion format looks like.
Now it is must that we have to include a base condition or we must have a base condition to
terminate the recursion. So here we can have more than one based conditions. So usually we have a
if condition where base condition would be included and a base value will be returned. And if we
have more than one base condition, then we can use else if statement and in the else block, we
usually will have a recursive call that is made with the help of return statement and some value
along with recursive call. Here is a program that implements recursion. Basically, this program is to
find the factorial of a number. Factorial of a number is nothing but the products of number. For
example, factorial of n is, will be equal to product of n to 1. So, for example, if we want to find the
factorial of four, it would be four into three into two into one. So here we are, trying to find the
factorial of a number by using a recursion technique. Now, the first line here is the function
declaration. Find Fact is a function that we would be defining later in the program, which returns the
value, whose return type is integer and also you will be using a data of type integer. Now, in the
main function, firstly, we have two variables which are declared of integer type. One is num and
another is fact. Now, here, num is two. Enter the value for N for which we want to find the factorial
and fact is a variable where we want to find the final result of the factorial. So that's the reason why
we're using these two variables. Now, once we declare, the next statement is to print by using print
F statement. We are asking the user to enter the value of an integer, where that value will be stored
in the variable num. So enter the value of an integer will be displayed on to the output console
where five would be entered or five is entered here. That five will be; five is stored in the variable
num. And now here is where we are placing the function call or making the function call. We're
calling find fact function, by passing the value num to it. That means to say that this function should
be finding the factorial for the num value which is five. So as soon as the control sees, the compiler
sees this function call, it will invoke the function and then the control will be jumped on to the
function call. Now here we have a define the find fact the function here. Now in this, if you can see,
if you observe, now the first line, if n equal to zero is the base condition that we're using. That
means to say that we will use this base condition to terminate.
That means if the number is zero, if n is zero, then we have to return a value one and terminate
recursion. Otherwise, if n is not equal to zero, then we are making use of a return statement to
recursively call the function. Here is where we are placing a recursive call to a function, that means
we are calling the function itself again by decrementing the value of n every time. So, this is a logic
where first five would be called, then it should become four and then should get multiplied with five
and it should be three and then should be multiplied by five into four. So the technique goes on this
way. So we are; in this return statement we are saying n into find factorial of n minus one. So we're
calling the function recursively here. Here is the tracing of the factorial program that is shown with
the help of recursion. Now the integer value five will be passed to the factorial function. The logic
runs like if one is equal to one or if n is equal to one, then return one; else return n into n, factorial n
minus one. Now five is compared with one here. It is false. So, control would jump to else part.
Wherein the return statement we are making a recursive call along with sum value. So, as it is n into
factorial n minus one, n value is five and n minus one value would be four. Now, when the function
call is made, it is a recursive function call that is made.
The control will get back to the function again with a new value four here. Now four is compared.
Now, it is false. So, control will come to else part. Now return will have to return n into factorial n
minus one. Now this value will be returned back which includes the recursive function call. So the
control will jump to the function again where three will be the new value, three will be compared
with one. It is false. So control will come to the else part where three into factorial two should be
calculated. There is a recursive function call here again So with the new value two, control will jump
to the factorial function again with the new value two and two will be compared with one, because
it is a false. Now control will come to the else part. Now two into factorial one has to be calculated.
Factorial, because the recursive call is made again, the function will jump, transfer the control to
itself again. Now we have to calculate factorial one. Now here the base condition is met. One is
equal to one. Now this would return one. Now where would it return? To the calling function. Where
has it been called from? The function has been called from this part of the program. That means
factorial one has called; factorial two has called factorial one.
So factorial one will return to; return the value to factorial two here, where one will be, one will be
returned. So two into one is calculated, two into one is two. So now the two value, two will be
returned to factorial. You know the calling function with the value factorial int three. So now here
the new value will be returned, which is two. Now three into two would be six that is returned to the
calling function. Now four into six is 24. That is returned back to the calling function. So, this is how
the tracing of factorial will work. The next famous game, which is called as Tower of Hanoi, is also
implemented by recursion technique. Now, this is a game where one disk has to be moved from one
pillar to the other pillar. We have three pillars here. One is a source pillar, another is a destination
pillar and another is a temporary pillar. We have to move all the disks from the source pillar to a
destination pillar by using temporary pillar. So the number of pillars that we have will be three and
we can have any number of disks. Now let us name these pillars as S for source pillar, D for
destination pillar, T for temporary pillar. There are certain rules that we have to follow for
implementing TOH game.
One first condition is only one disc can be moved from one pillar to another pillar at one point of
time. Second condition is that at no point of time, larger discs can be placed onto the smaller disk.
Now, the third thing is only top disk on a pillar can be moved to any other pillar. Discs, then from
source to destination, we can move it through the help of an auxiliary pillar or a temporary pillar. So
let us say we have two discs and a smaller disc is placed on the larger disk. We have to move these
discs from source to destination in the same order, and we can make use of an auxiliary pillar for
this. So in case we have n is equal to two, what we would do is the top disk of the source pillar,
which is the smaller disk will be moved to the auxiliary pillar. So, the move here will be S to A and we
only have one disc that is left in source pillar, which is the larger disk. That disk would be moved to
the destination pillar. So the move will here be S to D. And then now the top disk that is there in the
auxiliary pillar which is the smaller disk that would be placed back to the destination pillar. So A to D
is the next move. So the sequence here; S to A, S to D, and A to D. Instead of A we can even use T as
the letter for temporary pillar. Now, this is how the towers of Hanoi would look like. We have the
smallest disk on the top and largest disk at the end are in the last. So we have to move all these
discs. All these discs should be moved to the destination pillar in the same order by using the axillary
pillar.
So, these are the moves that we can have when the number of discs are three. So, we can see that
with these moves, all the three disks from the source pillar will be moved to the destination pillar.
And whenever we do that, we can make use of the auxiliary pillar. So, to generalize the procedure
what we do is, to write the logic of the function. These are the steps that can be followed. Firstly, we
moved the upper n minus one discs from source to auxiliary. That is all the discs which are above the
larger disk, which are above the last disc, has to be moved from the source to the auxiliary. And then
we move the largest disc, which is the last disc in the source that has to be moved from source to
the destination as that's the only disc which we would be, which would be remaining when we move
all the n minus one discs from source to auxiliary, then we move all the n minus one discs from
auxiliary to the destination peg. So here is the procedure that we can write in general, inside in the
function. So here we have, function by name move, which has parameters as N minus one, S comma
D. That means to say that we are moving all the n minus one discs from source to auxiIlary. Now, in
this context, the source pillar will be source and auxiliary pillar will be the destination so we can
make use of the destination pillar as an auxiliary pillar.
Next, we would be moving the large disc, last disc, which is in source pillar that is one disc from
source to the destination. Now, at this time we can make use of the auxiliary pillar as a temporary
pillar. Then we have move n minus one. All the n minus one discs will be moved from auxiliary to
destination. In this process, we can use S, that its source as an auxiliary pillar or a temporary pillar.
So here is the tracing that can be seen for TOH. The number of discs that we have taken here are
three. And we have S, A, D pillars. Now, this function, will have three steps. One is to move n minus
one discs from source to auxiliary, to move one disk from source to destination. And to move all n
minus one discs back from auxiliary to destination. So we have these three steps or these three
function calls. Now, in these three function calls again, when we have two discs, this would be
recursively called as again n minus one discs from source to auxiliary and one disk from source to
destination. And n minus one discs from source; auxiliary to destination. So, this is how the tracing of
the Towers of Hanoi can be seen. Thank you as we have reached the end of the session.