Professional Documents
Culture Documents
Int Function (Int Value) (If (Value 1) Return Function (Value - 1) Cout (Value) )
Int Function (Int Value) (If (Value 1) Return Function (Value - 1) Cout (Value) )
Int Function (Int Value) (If (Value 1) Return Function (Value - 1) Cout (Value) )
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.
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);
}
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
#include <iostream>
int fact(int n) {
if ((n==0)||(n==1))
return 1;
else
return n*fact(n-1);
int main() {
int n = 4;
return 0;
03 Handout Page 2 of 3
DATA STRUCTURES AND ALGORITHM
References:
Data Structure - Recursion Basics. Retrieved February 17, 2020 at
https://www.tutorialspoint.com/data_structures_algorithms/recursion_basics.htm
03 Handout Page 3 of 3