BY: ABHISHEK BHARDWAJ
PGT- COMPUTER SCIENCE
What is the function recursion in Python?
 Python also accepts function recursion, which means
a defined function can call itself.
 It means that a function calls itself.
 This has the benefit of meaning that you can loop
through data to reach a result.
Advantages of Recursion :
 Recursive functions make the code look clean and
elegant (Attractive).
 A complex task can be broken down into simpler sub-
problems using recursion.
 Sequence generation is easier with recursion than
using some nested iteration.
Disadvantages of Recursion :
 Sometimes the logic behind recursion is hard to follow
through.
 Recursive calls are expensive (inefficient) as they take
up a lot of memory and time.
 Recursive functions are hard to debug.
 Recursion are mainly of two types :
 When a function calls itself from within itself is
called direct recursion.
 When more than one function, call, one another
mutually is called indirect recursion.
 A recursion which leads to a infinite recursion is
termed as insensible recursion.
CODE SNIPPET :
1
2 3
Insensible Function Call
Base Case is a Condition/ Case, where, recursion stops.
To implement a recursion program, we have to prepare the base case first.
Recursion program implemented with Base Case is called right/ sensible
 Recursion limit is platform (Operating System) dependent.
CODE SNIPPET :
OUTPUT :
CODE SNIPPET :
OUTPUT :
BASE CASE :
4
3
2
1
Start of Recursion
Base Case
Recursion
1
2 3
4+Sum(3)
6
3+Sum(2)
3
2+Sum(1)
1
RECURSION STEPS:
3
6
10
Base Case

Recursion in Python.pdf

  • 1.
  • 2.
    What is thefunction recursion in Python?  Python also accepts function recursion, which means a defined function can call itself.  It means that a function calls itself.  This has the benefit of meaning that you can loop through data to reach a result.
  • 3.
    Advantages of Recursion:  Recursive functions make the code look clean and elegant (Attractive).  A complex task can be broken down into simpler sub- problems using recursion.  Sequence generation is easier with recursion than using some nested iteration. Disadvantages of Recursion :  Sometimes the logic behind recursion is hard to follow through.  Recursive calls are expensive (inefficient) as they take up a lot of memory and time.  Recursive functions are hard to debug.
  • 4.
     Recursion aremainly of two types :  When a function calls itself from within itself is called direct recursion.  When more than one function, call, one another mutually is called indirect recursion.
  • 5.
     A recursionwhich leads to a infinite recursion is termed as insensible recursion. CODE SNIPPET : 1 2 3 Insensible Function Call Base Case is a Condition/ Case, where, recursion stops. To implement a recursion program, we have to prepare the base case first. Recursion program implemented with Base Case is called right/ sensible
  • 6.
     Recursion limitis platform (Operating System) dependent. CODE SNIPPET : OUTPUT :
  • 7.
    CODE SNIPPET : OUTPUT: BASE CASE : 4 3 2 1 Start of Recursion Base Case Recursion 1 2 3 4+Sum(3) 6 3+Sum(2) 3 2+Sum(1) 1 RECURSION STEPS: 3 6 10 Base Case