Artikel Terbaru

Recursion

Introduction to C Recursion


Introduction to C Recursion
C language में recursion एक ऐसी process होती है जिसमें एक function खुद को ही call करता है। ऐसा किसी problem को small parts में divide करके solve करने के लिए किया जाता है। ऐसे functions जो स्वयं को call करते है उन्हें recursive functions कहा जाता है।


मान लीजिये आप किसी problem को solve करने के लिए कोई function create कर रहे है और इसका जो initial result है वो end result नहीं है। End result प्राप्त करने के लिए आपको इस process को वापस repeat करने की आवश्यकता है।

ऐसी situation में आप same process execute करने के लिए iteration करने के बजाय इसी function को शुरूआती result के साथ call करते है। जब तक end result नहीं प्राप्त हो जाता यह process चलती रहती है। End result को condition द्वारा determine किया जाता है।



End result प्राप्त होने पर इस process को terminate कर दिया जाता है। यदि इस function को terminate नहीं किया जाता है तो ये infinite time तक चलता जायेगा।



Advantages of Recursion 

  1. जब आप recursion के द्वारा किसी problem को solve करते है तो आपको बार बार function को call करने की जरुरत नहीं होती है। आप सिर्फ एक बार function को call करते है और end result नहीं आने तक वह स्वयं ही call होता रहता है। 
  2. Iteration के द्वारा problems को solve करना बहुत complex रहता है लेकिन recursion में same problem बहुत ही आसानी से solve हो जाती है।     


Structure of Recursive Function

एक recursive function का general structure निचे दिया जा रहा है।



return type myFunction(Parameters-list)
{
     statement 1;
     statement 2;
     ...
     statement N;

     myFunction(Arguments-list);  //Calling it self , Recursive process.

     ...

     other statements;



Disadvantages of Recursion 

हालाँकि recursion के द्वारा problem कम code में आसानी से solve की जा सकती है लेकिन recursive process की कुछ disadvantages भी होती है। इनके बारे में निचे दिया जा रहा है।   
  1. Slower - Recursive programs साधारण programs से slow होते है। Function को बार बार call होने के लिए और end result show करने में recursive programs को normal programs से अधिक समय लगता है। 
  2. Requires extra memory - साधारण programs की तुलना में recursive programs को execute होने के लिए extra memory की आवश्यकता होती है।     


Types of Recursion

C language में recursion 2 type का होता है। दोनों types के बारे में निचे detail से बताया जा रहा है। 

Direct Recursion

Direct recursion वह recursion होता है जिसमें एक function स्वयं को call करता है। इस तरह के recursion का general structure निचे दिया जा रहा है।



return_type MyFunction(parameter-list)

{

      ....statements;

      MyFunction(argument-list);

      .....other statements;



}

Indirect Recursion

Indirect recursion वह recursion होता है जिसमें एक function किसी दूसरे ऐसे function को call करता है जो वापस उसे ही call करता है। इस तरह के recursion का general structure निचे दिया जा रहा है।



First Function

return_type FunctionOne(parameter-list)

{

     ....statements;

     FunctionTwo(argument-list);

     ....other statements;

}


Second Function

return-type FunctionTwo(parameter-list)
{
       .....statements;
       FunctionOne(argument-list);
       .....other statements;
}


Example of Recursion (Factorial)

Recursion को example के द्वारा समझाने के लिए यँहा पर factorial का उदाहरण दिया जा रहा है। एक factorial number 1 से लेकर जिस भी number का आप factorial पाना चाहते है उस number तक के सभी numbers का  product (multiplication) होता है।



उदाहरण के लिए आप 5 का factorial calculate करना चाहते है। इसके लिए आपको 1 से लेकर 5 तक के सभी numbers को multiply करना होगा। सभी numbers को multiply करने के बाद जो result प्राप्त होगा वही 5 का factorial होगा।



1*2*3*4*5 = 120



C program के द्वारा किसी number का factorial calculate करने के दो तरीके हो सकते है।

  1. Using iteration - इस तरीके में आप 1 से लेकर दिए गए number तक loop चला सकते है और सभी numbers को multiply करके result print कर सकते है।   
  2. Using recursion - इस तरीके में आप एक function create कर सकते है जो दिए गए number को एक number कम से multiply करता है और स्वयं को उसी number के साथ call करता है।    
इन दोनों ही तरीकों को निचे उदाहरण के माध्यम से समझाया जा रहा है। 

Calculating Factorial of a Number Using Iteration

#include <stdio.h>

int main()
{
    int num,i;
    int result=1;
  
    printf("Enter number to find out its factorial.....");
    scanf("%d",&num);

    for(i=1; i<=num; i++)
     result = result*i;

     printf("\nFactorial of %d is %d",num,result);

     return 0;
}

ऊपर दिया गया program निचे दिया गया output generate करता है।

Enter a number to find out its factorial..

5

120

Calculating Factorial of a Number Using Recursion

#include <stdio.h>

int MyFactFunction(int n);

int num;

int main()
{
    printf("Enter a number to find its factorial...");
    scanf("%d",&num);

    printf("Factorial of %d is : %d",num,MyFactFunction(num));
    return 0;
}

int MyFactFunction(num)
{
    if(num >= 1)
        return num*MyFactFunction(num-1);
    else
        return 1;
}


ऊपर दिए गए उदाहरण में MyFactFunction एक recursive function है। ये दिए गए number से एक number कम के साथ वापस स्वयं को call करता है और original value से (जिसके साथ function को call किया गया था) multiply करता है।



ऐसा ये तब तक करता है जब तक original value 1 से अधिक होती है। जैसे ही यह value 1 हो जाती है, यह function terminate हो जाता है। ये program निचे  दिया गया output generate करता है।



Enter a number to find its factorial....
6
Factorial is : 720 

Post a Comment

0 Comments