2. INTRODUCTION TO RECURSION
1. A recursive function is one that calls itself.
2. It is one of the most powerful tool in programming
language. It is a process where function calls itself again and
again.
3. Recursion basically divides the big problem into small
problems up to the point where it can be solved easily, for
example if we have to calculate factorial of a 5, we will divide
factorial of 5 as 5*factorial (4), then 4*factorial (3), then
3*factorial (2), then 2*factorial (1) and now factorial of 1 can
be easily solved without any calculation, now each pending
function will be executed in reverse order.
3. RECURSION HAVE TWO PARTS
1.Base case(s) (non-recursive)
Recursion ends when the number reduces to 1. This is called
the base condition.
Every recursive function must have a base condition that
stops the recursion or else the function calls itself infinitely.
2. Recursive case(s)
The recursive calls should moves in such a way that each
time it comes closer to the base criteria.
By “simpler”, we mean “smaller” or “shorter” or “closer to the
base case”.
4. 12
IMPORTANT NOTES
By default, the maximum depth of recursion is 1000. If the limit is
crossed, it results in Recursion Error. Let's look at one such
condition.
def recursor():
recursor()
recursor()
Output
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "<string>", line 2, in a
File "<string>", line 2, in a
File "<string>", line 2, in a
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
5. HOW RECURSION WORKS?
• In execution of a program, the computer
creates function stack (LAST IN FIRST OUT).
• This function stack is a stack of frames.
• At every call of function, new parameter
value gets pushed to the stack. The top of the
stack always contains parameter value of last
call.
• When base case evaluates to true, values
from stack needs to pop out one by one and
result is to be returned after calculation.
6. RECURSION EXAMPLE
• A recursive function is one that calls itself.
def factorial(x):
if x==1: # BASE CASE
return 1
else:
return x*factorial(x-1) #R RECURSIVE CASE
Example:
f=factorial(5)
print ("factorial of 5 is ",f)
8. RECURSION CONCEPTUALLY
4! = 4(3!)
3! = 3(2!)
2! = 2(1!)
1! = 1 (0!)
Base case
make smaller instances
of the same problem
9. RECURSION CONEPTUALLY……
4! = 4(3!)
3! = 3(2!)
2! = 2(1!)
1! = 1 (0!) = 1(1) = 1
Compute the base case
make smaller instances
of the same problem
10. RECURSION CONCEPTUALLY…
4! = 4(3!)
3! = 3(2!)
2! = 2(1!) = 2
1! = 1 (0!) = 1(1) = 1
Compute the base case
make smaller instances
of the same problem
build up
the result
=6
24
11. Recursion makes our program:
1. Easier to write.
2. Readable – Code is easier to read and
understand.
3. Reduce the lines of code – It takes less
lines of code to solve a problem using
recursion.
ADVANTAGE OF RECURSION
12. 1.Not all problems can be solved using recursion.
2. If you don’t define the base case then the code
would run indefinitely.
3. Debugging is difficult in recursive functions as
the function is calling itself in a loop and it is hard
to understand which call is causing the issue.
4. Memory overhead – Call to the recursive
function is not memory efficient.
DIS-ADVANTAGE OF RECURSION