Recursion, Types of Recursion, Advantages and Disadvantages of Recursion in C language

When a function calls itself until terminating condition executed then this type of function calling is called Recursion. Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series,Tower of Hanoi etc.
We have to follow two rules for making a successful recursion process:-

1.) Terminating condition :- This is a statement or condition which will stop the recursive call, must be presented in a function otherwise function may enter inside infinite loop. 

2.) Recursive call :- one or more statements which call function recursively, must be presented in a function.

เคœเคฌ เคเค• เคซंเค•्เคถเคจ เคŸเคฐ्เคฎिเคจेเคŸिंเค— เค•ंเคกीเคถเคจ เค•े เค†เคจे เคคเค• เคธ्เคตเคฏं เค•ो เค•ॉเคฒ เค•เคฐเคคा เคฐเคนเคคा เคนै เคคเคฌ เค‡เคธ เคช्เคฐเค•ाเคฐ เค•ी เคซंเค•्เคถเคจ เค•ाเคฒिंเค— เค•ो เคฐिเค•เคฐ्เคธเคจ เค•เคนा เคœाเคคा เคนै। เคฐिเค•เคฐ्เคธिเคต เคซंเค•्เคถเคจ เค•े เคฆ्เคตाเคฐा เค•เคˆ เค—เคฃिเคคीเคฏ เคธเคฎเคธ्เคฏाเค“ เค•ो เคนเคฒ เค•िเคฏा เคœाเคคा เคนै เคœैเคธे เฅžैเค•्เคŸोเคฐिเคฏเคฒ, เคซिเคฌोเคจाเค•्เค•ी เคธीเคฐीเคœ, เคŸाเคตเคฐ เค‘เฅž เคนเคจोเคˆ เค‡เคค्เคฏाเคฆि। 
เคเค• เคธเคซเคฒ เคฐिเค•เคฐ्เคธเคจ เคช्เคฐोเคธेเคธ เคคैเคฏाเคฐ เค•เคฐเคจे เค•े เคฒिเค เคนเคฎे เคจिเคฎ्เคจ เคฆो เคจिเคฏเคฎो เค•ा เคชाเคฒเคจ เค•เคฐเคจा เค…เคจिเคตाเคฐ्เคฏ เคนै -

1.) เคŸเคฐ्เคฎिเคจेเคŸिंเค— เค•ंเคกीเคถเคจ:- เคฏเคน เคเค• เคตिเคถेเคท เคธ्เคŸेเคŸเคฎेंเคŸ เคฏा เค•ंเคกीเคถเคจ เคนोเคคी เคนै, เคœो เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เค•ो เคคเคค्เค•ाเคฒ เคธเคฎाเคช्เคค เค•เคฐ เคฆेเคคी เคนै เค‡เคธเค•ा เคซंเค•्เคถเคจ เคฎें เคนोเคจा เค…เคจिเคตाเคฐ्เคฏ เคนै เค…เคจ्เคฏเคฅा เคซंเค•्เคถเคจ เค‡เคจเคซिเคจिเคŸ เคฒूเคช เคฎें เคœा เคธเค•เคคा เคนै। 

2.) เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ :- เคเค• เคฏा เค…เคงिเค• เคธ्เคŸेเคŸเคฎेंเคŸ เคœो เคซंเค•्เคถเคจ เค•ो เคฌाเคฐ-เคฌाเคฐ เค•ॉเคฒ เค•เคฐเคคे เคฐเคนเคคे เคนै เค‡เคจเค•ा เคซंเค•्เคถเคจ เคฎें เคนोเคจा เค…เคจिเคตाเคฐ्เคฏ เคนै।  

Syntax:-
void func()
{
    ... .. ...
    func();
    ... .. ...
}
int main()
{
    ... .. ...
    func();
    ... .. ...
}

Example Code:-

long factorial(long f){
if(f==0) return 1;
return f*factorial(f-1);
}

Winding Process:

Function called Function return

Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1

Unwinding Process

Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1

Types of Recursion:-
เคฐिเค•เคฐ्เคธเคจ เค•े เคช्เคฐเค•ाเคฐ:-
 

1. Tail recursion:- when a recursive call is the last statement of function and there is no more statement(s) left to execute then it is called tail recursion.
1. เคŸेเคฒ เคฐिเค•เคฐ्เคธเคจ:- เคœเคฌ เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคซंเค•्เคถเคจ เค•ा เค…ंเคคिเคฎ เคธ्เคŸेเคŸเคฎेंเคŸ เคนो เค…เคฐ्เคฅाเคค เค•ोเคˆ เคธ्เคŸेเคŸเคฎेंเคŸ เคฐเคจ เคนोเคจा เคถेเคท เคจा เคนो เคคเคฌ เค‡เคธे เคŸेเคฒ เคฐिเค•เคฐ्เคธเคจ เค•เคนा เคœाเคคा เคนै। 

2. Non-tail recursion :- when a recursive call is not the last statement of function and there is one or more statements left to execute then it is called non-tail recursion.
2. เคจॉเคจ-เคŸेเคฒ เคฐिเค•เคฐ्เคธเคจ:- เคœเคฌ เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคซंเค•्เคถเคจ เค•ा เค…ंเคคिเคฎ เคธ्เคŸेเคŸเคฎेंเคŸ เคจ เคนो เค…เคฐ्เคฅाเคค เค•ोเคˆ เคธ्เคŸेเคŸเคฎेंเคŸ เคฐเคจ เคนोเคจा เคถेเคท เคนो เคคเคฌ เค‡เคธे เคจॉเคจ-เคŸेเคฒ เคฐिเค•เคฐ्เคธเคจ เค•เคนा เคœाเคคा เคนै। 

3. Direct recursion :- when recursive call will directly call function then it is called direct recursion. It means a function has recursive call to itself.
3. เคกाเคฏเคฐेเค•्เคŸ เคฐिเค•เคฐ्เคธเคจ:- เคœเคฌ เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคธ्เคตเคฏं เค•ो เคช्เคฐเคค्เคฏเค•्เคท เคฐूเคช เคธे เค•ॉเคฒ เค•เคฐเคคी เคนै เคคเคฌ เค‡เคธे เคกाเคฏเคฐेเค•्เคŸ เคฐिเค•เคฐ्เคธเคจ เค•เคนा เคœाเคคा เคนै।  

4. Indirect recursion:- when a function (F1) has recursive call of function(F2) then function (F2) has recursive call of function (F1) then it is called indirect recursion.It means a function has no recursive call to itself.
4. เค‡เคจเคกाเคฏเคฐेเค•्เคŸ เคฐिเค•เคฐ्เคธเคจ:- เคœเคฌ เคซंเค•्เคถเคจ F1 เค•ी เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคซंเค•्เคถเคจ F2 เค•ो เค•ॉเคฒ เค•เคฐเคคी เคนै เคเคตं เคซंเค•्เคถเคจ F2 เค•ी เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคซंเค•्เคถเคจ F1 เค•ॉเคฒ เค•เคฐเคคी เคนै เคคเคฌ เค‡เคธे เค‡เคจเคกाเคฏเคฐेเค•्เคŸ เคฐिเค•เคฐ्เคธเคจ เค•เคนा เคœाเคคा เคนै เค…เคฐ्เคฅाเคค เคซंเค•्เคถเคจ เค•े เคชाเคธ เคธ्เคตเคฏं เค•ा เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคจเคนीं เคนै।   

5. Simple recursion :- if there is only one function calling statement exist in a recursive call then it is called simple recursion.
5. เคธिंเคชเคฒ เคฐिเค•เคฐ्เคธเคจ:- เคœเคฌ เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคฎें เค•ेเคตเคฒ เคเค• เคซंเค•्เคถเคจ เค•ाเคฒिंเค— เคธ्เคŸेเคŸเคฎेंเคŸ เคนो เคคเคฌ เค‡เคธे เคธिंเคชเคฒ เคฐिเค•เคฐ्เคธเคจ เค•เคนा เคœाเคคा เคนै। 

6. Tree recursion :- if there are more than one function calling statements exist in recursive call then it is called tree recursion.
6. เคŸ्เคฐी เคฐिเค•เคฐ्เคธเคจ:- เคœเคฌ เคฐिเค•เคฐ्เคธिเคต เค•ॉเคฒ เคฎें เคเค• เคธे เค…เคงिเค• เคซंเค•्เคถเคจ เค•ाเคฒिंเค— เคธ्เคŸेเคŸเคฎेंเคŸ เคนो เคคเคฌ เค‡เคธे เคŸ्เคฐी เคฐिเค•เคฐ्เคธเคจ เค•เคนा เคœाเคคा เคนै।

Advantages of Recursion:-
เคฐिเค•เคฐ्เคธเคจ เค•े เคฒाเคญ :-
1. It makes program elegant and cleaner.
1. เคฏเคน เคช्เคฐोเค—्เคฐाเคฎ เค•ो เคตिเคถिเคท्เคŸ เคเคตं เคธ्เคชเคท्เคŸ เคฌเคจเคคा เคนै। 
2. All algorithms can be defined recursively which makes it easier to visualize and prove. 
2. เคธเคญी เค…เคฒ्เค—ोเคฐिเคฅเคฎ เค•ो เคฐिเค•เคฐ्เคธिเคต เคฌเคจाเคฏा เคœा เคธเค•เคคा เคนै เคœो เค‡เคธे เคธเคฐเคฒ เคเคตं เคธเคฎเคเคจे เคฎें เค†เคธाเคจ เคฌเคจเคคा เคนै।  

Disadvantages of Recursion:-
เคฐिเค•เคฐ्เคธเคจ เค•ी เคนाเคจिเคฏाँ:-
1. If the speed of the program is very important then, we should avoid using recursion. 
1. เคฏเคฆि เคช्เคฐोเค—्เคฐाเคฎ เค•ी เค—เคคि เค…เคคिเคฎเคนเคค्เคตเคชूเคฐ्เคฃ เคนै เคคเคฌ เคฐिเค•เคฐ्เคธเคจ เค•ा เคช्เคฐเคฏोเค— เคจเคนीं เค•เคฐเคจा เคšाเคนिเค।
2. It uses more memory and are generally slow.
2. เคฏเคน เค…เคค्เคฏเคงिเค• เคฎेเคฎोเคฐी เค•ा เคช्เคฐเคฏोเค— เค•เคฐเคคा เคนै เคเคตं เคงीเคฎी เค—เคคि เคธे เค•ाเคฐ्เคฏ เค•เคฐเคคा เคนै।  

Comments

Popular posts from this blog

C Language Topics in Hindi and English

C language IMP Questions for BSc/BA/BCom/BCA/BE/BTech/MSc/MCA (CS/IT) I year students