Int Function (Int Value) (If (Value 1) Return Function (Value - 1) Cout (Value) )

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

DATA STRUCTURES AND ALGORITHM

What is Recursion?
It is the process in which a function calls itself directly or indirectly and the corresponding
function is called as recursive function. Using recursive algorithm, certain problems can be
solved quite easily.

Two (2) ways to use a recursive function:

Example − a function calling itself (direct)


int function(int value) {
if(value < 1)
return;
function(value - 1);
cout << (value);
}

Example − a function that calls another function which in turn calls it again (indirect)
int function1(int value1) {
if(value1 < 1)
return;
function2(value1 - 1);
cout << (value1);
}

int function2(int value2) {


function1(value2);
}

Properties of a recursive function:


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, when this
condition is met the function stops calling itself 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.

03 Handout Page 1 of 3
DATA STRUCTURES AND ALGORITHM

How memory is allocated to different function calls in recursion?


When any function is called from main(), the memory is allocated to it on the stack. A recursive
function calls itself, the memory for a called function is allocated on top of memory allocated to
calling function and different copy of local variables is created for each function call. When the
base case is reached, the function returns its value to the function by whom it is called and
memory is de-allocated and the process continues.
Below program show how to create and make use of a recursion function in C++.

//solving Factorial of a natural number using recursion

#include <iostream>

using namespace std;

int fact(int n) {

if ((n==0)||(n==1))

return 1;

else

return n*fact(n-1);

int main() {

int n = 4;

cout<<"Factorial of "<<n<<" is "<<fact(n);

return 0;

What are the disadvantages of recursive programming over iterative programming?


Note that both recursive and iterative programs have the same problem-solving powers, i.e.,
every recursive program can be written iteratively and vice versa is also true. The recursive
program has greater space requirements than iterative program as all functions will remain in
the stack until the base case is reached. It also has greater time requirements because of
function calls and returns overhead.

03 Handout Page 2 of 3
DATA STRUCTURES AND ALGORITHM

What are the advantages of recursive programming over iterative programming?


Recursion provides a clean and simple way to write code. Some problems are inherently
recursive like tree traversals, Tower of Hanoi, etc. For such problems, it is preferred to write
recursive code. We can write such codes also iteratively with the help of a stack data structure.

References:
Data Structure - Recursion Basics. Retrieved February 17, 2020 at
https://www.tutorialspoint.com/data_structures_algorithms/recursion_basics.htm

Recursion. Retrieved February 17, 2020 at https://www.geeksforgeeks.org/recursive-


functions/

03 Handout Page 3 of 3

You might also like