Recursion

Recursion refers to a function calling the same function from inside it . It is useful in solving certain kinds of problems whose solution can be more elegantly written using recursion.

A recursive function needs a base condition where the function returns without calling itself.

Ex: stack1.cpp

#include <iostream>

using namespace std ;

void stack1()

{

int arr1[1000000] ;

cout << "Inside stack1 function." << endl ;

stack1() ;

}

//------------------------------------------

int main()

{

stack1() ;

return( 0 ) ;

}

Output:

[amittal@hills Recursion]$ ./a.out

Inside stack1 function.

Inside stack1 function.

Segmentation fault (core dumped)

[amittal@hills Recursion]$

[amittal@hills Recursion]$

We have a function "stack1" that