Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
"The problem is solved by repetitively breaking it
into smaller problem which are similar in nature to
the original problem."
In this tutorial we will discuss the following topics
S. No. Topics
1 What is Recursion in programming?
2 Properties of Recursion
3 Recursion – To print message forever
Program to Display Factorial of given number using
4
Recursion
5 Program to Display Fibonacci Series Using Recursion
Program to Display sum of first n natural numbers, using
6
Recursion
Program using Recursion to search element from an array
7
using Binary Search .
8 Recursion vs Iteration
Page 1 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
What is Recursion in programming?
Recursion means when a piece of code/method calls itself over & over
till a condition holds true.
Recursion is a process in which a problem is define in terms of itself
Advantages of Recursion:
1. Reduce unnecessary calling of function.
2. Through Recursion one can solve problems in easy way while its
iterative solution is very big and complex. For example to reduce the code
size for Tower of Honai application, a recursive function is best suited.
3. Extremely useful when applying the same solution.
Disadvantages of Recursion:
1. Recursive solution is always logical and it is very difficult to trace.
(debug and understand).
2. In recursive we must have if statement somewhere to force the
function to return without the recursive calls being executed, otherwise
the function will never return.
3. Recursion takes a lot of stack space, usually not considerable when the
program is small and running on a PC.
4. Recursion uses more processor time.
Page 2 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
Properties of Recursion
A recursive function can go infinite like a loop. To avoid infinite running
of recursive function, there are two properties that a recursive function
must have −
Base criteria − There must be at least one base criteria or condition,
such that, the recursive calls must reach a base case condition,
which is solved without further recursion.
Base condition is specified using ‘if’ to specify the termination
Condition before using the function recursively.
Progressive approach −The recursive calls should progress in such a
way that each time a recursive call is made it comes closer to the
base criteria.
Recursion – To print message forever
If a recursion never reaches a base case, it goes on making recursive calls
forever, and the program never terminates. This is known as infinite
recursion, and it is generally not considered a good idea. Here is a
minimal program with an infinite recursion:
def message(): In this code, base
condition is missing so
print(“Hi, I’m there”)
message () function will
call itself again and again
till out of memory error
message()
Page 3 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
Program to Display Factorial of given number using
Recursion
Lets write a recursive solution to find N! (or N factorial):
N factorial is defined as: N! = N * (N-1) * (N-2)…… * 2 * 1.
For example, 4! = 4 * 3 * 2 * 1
Our base case for this example is N = 0 as 0! is defined as 1.
So lets write our function to find the factorial of given number:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
Working:
We’ll call our function and
pass 4 in for n factorial (4) calls
factorial (3) ….. which calls
factorial(0) which is our base
case so we return 1 back to
the previous call of factorial(1)
which takes our 1 and multiplies it by 1 which evaluate to 1 which passes
that back to factorial(2) which takes our 1 and multiplies it by 2 which
evaluates to 2 which passes that back to factorial(3) which multiplies 3 *2
to get 6 which passes that back to factorial(4) which multiples 4 * 6 which
evaluates to 24 which is returned as our answer.
Page 4 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
Program to Display Fibonacci Series Using Recursion
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....
The first two terms are 0 and 1. All other terms are obtained by adding
the preceding two terms. This means to say the nth term is the sum of
(n-1)th and (n-2)th term.
Page 5 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
ANALYSIS
In this Python Fibonacci Series program example, we defined a function.
The following function accepts integer values as parameter value and
return value.
def fibonacci(n):
Let’s see the elif statement inside the above-specified functions
if (n == 0) check whether the given number is 0 or not. If it is TRUE, the function
returns the value Zero.
if(n == 1) check whether the given number is 1 or not. If it is TRUE, function
return the value One.
And, if the number is greater than 1, the statements inside the else block
executed.
Within the Else block of this Python Fibonacci series, we called the function
recursively to display the Fibonacci series.
return (fibonacci (n - 2)+ fibonacci (n - 1))
Page 6 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
Program to Display sum of first n natural numbers, using
Recursion
Initially, sumNumbers() is called with n=5 passed as an argument.
The number 5 is added to the result of sumNumbers(4).
In the next function call from sumNumbers() to sumNumbers(), 4 is
passed which is added to the result of sumNumbers(3). This process
continues until n is equal to 0.
When n is equal to 0, there is no recursive call. This returns the sum
of integers.
Page 7 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
Program using Recursion to search element from an array
using Binary Search .
Page 8 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-5 Recursion
Recursion vs Iteration
Recursion:
1. Recursion is the technique of defining any term in terms of itself.
2. Not all problems have recursive solutions.
3. There may be an exclusively if-statement inside the recursive
function, specifying stopping condition.
4. Recursive is generally a worse option to go for simple problems, or
problems not recursive in nature.
Iteration:
1. It is a process of executing a statement or a set of statement
repeatedly, until some specific condition is specified.
2. Any recursive problem can be solved iteratively.
3. Iteration involves clear cut-steps initialization, condition, execution,
updation.
4. Iterative counterpart of a problem is more efficient in terms of
memory utilization and execution speed.
Page 9 of 9