Kabir Sahu
RECURSION Class 12 A
Kendriya Vidyalaya Dinjan
WHAT IS RECURSION ?
• Recursion refers to a programming technique in which a function
calls itself either directly or indirectly.
• Recursion is a technique for solving a large compositional problem
by repeatedly applying the same procedures to reduce it to
successively smaller problems.
• A recursive procedure has two parts: one or more base cases and a
recursive step.
RECURSIVE FUNCTION
• A function is said to be recursive function if it calls itself. Eg :-
def A( ) :
A( )
• Direct recursion : If a function calls itself directly from its body.
• Indirect recursion : If a function calls another function, which calls
its caller function.
• Right or Sensible Recursive code - It must have a case, whose
result is computed without any recursive calling and it also have
Recursive Case.
RECURSION IN PYTHON
• A Recursive Definition is a definition that is made in terms of smaller
version of itself.
xⁿ = x*(xⁿ⁻¹) for n>1 (Recursive definition)
xⁿ = x for n=1
xⁿ = 1 for n=0
• Every recursive function must have at least following two cases :
i) The Recursive Case (or the inductive case)
ii) The Base Case (or the stopping case) - Always Required
• Infinite Recursion : An infinite recursion is when a recursive function calls
itself endlessly. Eg- if b == 0 :
return 1
Infinite recursion can happen in in one of the following cases :
i) Base Case Not Reached.
ii) Base Case Not Defined.
• Iterative Version : A recursive code may also be written in non recursive
way, which is the iterative version of the same.
• Some Recursive Codes
A. Computing Factorial Recursively : The factorial of a non negative
number is defined as the product of all the values from 1 to the number : n! =
1*2*…*n
Eg:
def factorial (n) :
if n < 2 :
return 1
return n * factorial(n-1)
B. Computing GCD Recursively : If a > b then the GCD of a and b is the
same as the GCD of a and a % b. Eg :
def gcd (a, b) :
if b==0 :
return a
else:
return gcd(b, a % b)
C. Fibonacci numbers : A Fibonacci sequence is the integer sequence of
0,1,2,3,5,8…The first two terms are 0 and 1. All other terms are obtained by
adding the preceding two terms. Eg:-
def fib(n):
if n == 1 :
return 0
elif n == 2 :
return 1
else :
Binary Search :
• Binary search can work for only sorted arrays whereas linear search can
work for both sorted as well as unsorted arrays.
• beg, last signify the limits of the search segment. Sometimes beg,last
are also termed as low and high to signify lower and higher limit of the
search segment.
• To implement binary search on an array -
i) The given array or sequence must be sorted.
ii) Its sort order and size must be known.
RECURSION VS ITERATION
• Recursion makes the code short and simple while iteration makes the
code longer comparatively.
• Recursion is slower than iteration due to overhead of multiple function
calls and maintaining a stack for it.
• In iteration, the code is executed repeatedly using the same memory
space. That is, the memory space allocated once, is used for each pass
of the loop.