You are on page 1of 10

What is Recursion?

Need of Recursion
Properties of Recursion
How are Recursive functions stored in memory?
What is the base condition in recursion?
Format for Base Condition
Why does Stack Overflow error occur in recursion?
What is the difference between direct and indirect
recursion?
Format for direct recursion
Format for indirect recursion
What is tail recursion?
Why do we care about tail recursion?
Problems
Q1: Write a Program and Recursive Relation to find
the Fibonnaci series of n.

1
Recursion
What is Recursion?

The process in which a function calls itself directly or


indirectly is called recursion and the corresponding function
is called a recursive function. Using a recursive algorithm,
certain problems can be solved quite easily.

The idea is to represent a problem in terms of one or more


smaller problems, and add one or more base conditions
that stop the recursion. For example, we compute factorial
n if we know the factorial of (n-1). The base case for
factorial would be n = 0. We return 1 when n = 0.

Need of Recursion
● Reduce the length of our code and make it easier to
read and write.

● It has certain advantages over the iteration technique

● A task that can be defined with its similar subtask,


recursion is one of the best solutions for it.

Properties of Recursion
● Base condition is needed to stop the recursion
otherwise infinite loop will occur.

2
● Performing the same operations multiple times with
different inputs.

How are Recursive functions stored in


memory?

Recursion uses more memory, because the recursive


function adds to the stack with each recursive call, and
keeps the values there until the call is finished. The
recursive function uses LIFO (Last In Fast Out) structure
just like the stack data structure.

What is the base condition in recursion?

The role of the base condition is to stop a recursive function


from executing endlessly – once a pre-specified base
condition is met, the function knows it’s time to exit.

In a recursive function, the solution to the base case is


provided and the solution of the bigger problem is
expressed in terms of smaller problems.

Format for Base Condition

int fact(int n){

3
// base case
if(n <= 1){
return 1;
}

// smaller problem of a bigger problem


else{
return n*fact(n-1);
}
}

Why does Stack Overflow error occur in


recursion?

If the base case is not reached or not defined, then the


stack overflow problem may arise.

What is the difference between direct and


indirect recursion?

A function fun is called direct recursive if it calls the same


function fun.

4
Format for direct recursion

void directRecFun()
{
// Some code....

directRecFun();

// Some code...
}

A function fun is called indirect recursive if it calls another


function, say fun_new and fun_new calls fun directly or
indirectly.

Format for indirect recursion

void indirectRecFun1()
{
// Some code...

indirectRecFun2();

// Some code...
}
void indirectRecFun2()
{
// Some code...

5
indirectRecFun1();

// Some code...
}

What is tail recursion?

A recursive function is tail recursive when a recursive call is


the last thing executed by the function.

Why do we care about tail recursion?

The tail recursive functions are considered better than non


tail recursive functions as tail-recursion can be optimized by
the compiler. Compilers usually execute recursive
procedures by using a stack. This stack consists of all the
pertinent information, including the parameter values, for
each recursive call. When a procedure is called, its
information is pushed onto a stack, and when the function
terminates the information is popped out of the stack. Thus
for the non-tail-recursive functions, the stack depth
(maximum amount of stack space used at any time during
compilation) is more. The idea used by compilers to
optimize tail-recursive functions is simple, since the
recursive call is the last statement, there is nothing left to do
in the current function, so saving the current function’s stack
frame is of no use .

6
7
Problems

Write a Program and Recursive Relation to


find the Fibonnaci series of n.

Code In C++ :

#include<iostream>
using namespace std;

int fibonacci(int n){

// Base case 1
if( n == 0 ){
return 0;
}

// Base case 2
if( n == 1 || n == 2 ){

return 1;
}

// Recursive function
else{
return (fibonacci(n-1) + fibonacci(n-2));
}
}
int main(){

8
cout << "Enter the number of iteration to
occur :" << endl;
int n;
cin>>n;

for (int i = 0; i < n; i++)


{
cout << fibonacci(i) << " ";
}
return 0;
}

9
10

You might also like