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
Post a Comment