R e c u r s I o n
By Nalin Adhikari
© Nalin Adhikari
Contents
 Introduction
 Recursive Function
 Advantages
 Disadvantages
 Examples
 Tower Of Hanoi (TOH)
Introduction
 Method of defining function/procedure in
which the function being defined is
applied within its own.
 It is the function in which it will call itself
repeatedly until a certain condition is
satisfy.
 It is related to the last in first out nature
of the stack.
 Stack is the appropriate mechanism to
store the values of the parameter of
recursion.
Recursive Function
 A function that calls itself during its
execution.
 It repeats itself several times until it’s
end condition.
Advantages
 To write efficient programs using a
minimum amount of code.
 Nominal chance of syntax and logical
errors.
 Increases the program efficiency.
Disadvantages
 Can cause infinite loops and other
unexpected results if not written
properly.
 If the end condition is not defined then
the recursion will repeat forever,
causing the program to crash or hang
the entire computer system.
Examples
 Factorial function
 Fibonacci sequence
 Multiplication of natural numbers
 Binary search
Tower Of Hanoi (TOH)
 It can be solved by using recursion
technique.
 There exist three peg namely A, B & C.
 Several disks of different diameters are
placed in peg A.
 The large disk is always below the
smaller one.
 The objective is to move those disks to
peg C, using peg B as auxiliary.
 Only the top disk can be moved to other
peg.
 Only one disk may be moved at a time.
Algorithm (TOH)
To move n disks from A to C, using B as
auxiliary
 If n=1, move the single disk from A to C and
stop.
 Move the top n-1 disks from A to B, using C
as auxiliary.
 Move the remaining disk from A to C.
 Move the n-1 disks from B to C, using A as
auxiliary.
THE END
© Nalin Adhikari

Recursion

  • 1.
    R e cu r s I o n By Nalin Adhikari © Nalin Adhikari
  • 2.
    Contents  Introduction  RecursiveFunction  Advantages  Disadvantages  Examples  Tower Of Hanoi (TOH)
  • 3.
    Introduction  Method ofdefining function/procedure in which the function being defined is applied within its own.  It is the function in which it will call itself repeatedly until a certain condition is satisfy.  It is related to the last in first out nature of the stack.  Stack is the appropriate mechanism to store the values of the parameter of recursion.
  • 4.
    Recursive Function  Afunction that calls itself during its execution.  It repeats itself several times until it’s end condition.
  • 5.
    Advantages  To writeefficient programs using a minimum amount of code.  Nominal chance of syntax and logical errors.  Increases the program efficiency.
  • 6.
    Disadvantages  Can causeinfinite loops and other unexpected results if not written properly.  If the end condition is not defined then the recursion will repeat forever, causing the program to crash or hang the entire computer system.
  • 7.
    Examples  Factorial function Fibonacci sequence  Multiplication of natural numbers  Binary search
  • 8.
    Tower Of Hanoi(TOH)  It can be solved by using recursion technique.  There exist three peg namely A, B & C.  Several disks of different diameters are placed in peg A.  The large disk is always below the smaller one.  The objective is to move those disks to peg C, using peg B as auxiliary.  Only the top disk can be moved to other peg.  Only one disk may be moved at a time.
  • 9.
    Algorithm (TOH) To moven disks from A to C, using B as auxiliary  If n=1, move the single disk from A to C and stop.  Move the top n-1 disks from A to B, using C as auxiliary.  Move the remaining disk from A to C.  Move the n-1 disks from B to C, using A as auxiliary.
  • 10.