0

What is the benefit to use recursion then loop(for,while,do-while)?

Using Recursion(Here i am getting the sum of a given number, suppose number is 5 then 5 + 4 + 3 + 2 + 1:

   #include<stdio.h>
   int sum(int n);
   void main()
    {
     int sums =0, n;
     scanf("%d",&n);
     sums = sum(n);
     printf("%d",sums);

       while (1)
        {

        }
    }

  int sum(int n)
  {
     if (n==1)
     return 1;
     return n + sum(n-1);
  }

Without recursion(Here i am getting the sum of a given number suppose number is 5 then 5 + 4 + 3 + 2 + 1:

  #include<stdio.h>

  void main()
   {
     int sum =0, n;
     scanf("%d",&n);
     for(int i=n;i>=1;i--)
      {
       sum = sum + i;
      }
      printf("%d",sum);

      while (1)
      {
      }
    }
Maroun
  • 94,125
  • 30
  • 188
  • 241
Joy Acharya
  • 680
  • 1
  • 10
  • 18
  • 1
    The answer is you don't need recursion for a simple example like this. – Mr Lister May 25 '15 at 10:33
  • Then when recursion is need. can u give me examples ??? – Joy Acharya May 25 '15 at 10:34
  • lol Why do you have infinite loops at the end of both the programs? – Spikatrix May 25 '15 at 10:38
  • 2
    Recursion used in those examples where iterative solutions are available serves but-one truly useful purpose: to teach recursion in academia. And with your "when recursion is need(ed)" addendum, it somewhat throws water on the very premise of your question, as with that you're essentially asking: "Why do we need recursion when iterative solutions exist, besides when they don't exist and we need recursion? – WhozCraig May 25 '15 at 10:40
  • 3
    @WhozCraig: summarizing, "recursion exists so students can be taught recursion" – Jongware May 25 '15 at 10:45
  • It works like getchar() so that i can hold my screen stand by @Cool Guy – Joy Acharya May 25 '15 at 10:46
  • very funny :P @Jongware – Joy Acharya May 25 '15 at 10:56

3 Answers3

7

You can always make a recursive function an iterative one, and vice versa (Turing said).

In some cases, it's better to use recursion (e.g. traveling on a tree), it's more natural to "think recursively" in such cases. However, if using loops isn't more complicated and much more difficult than a recursion, I prefer them.

Recursion is more costly in memory, but sometimes it clearer and a more readable, using loops increases the performance, but recursion can sometimes be better for the programmer (and his performance).

Deciding what to use - recursion or iteration, depends on what you want to implement, and what's more important for you (readability? performance?), asking recursion or iteration is somehow like asking elegance or performance.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 3
    + Traversing a tree makes a lot more sense recursively. Traversing a linked list can even be simpler with recursion of the operation is simple. Right tool for the right job. – Carcigenicate May 25 '15 at 10:52
3

recursion can solve all those problems which can be solved by for loop. But it is difficult or quite impossible to solve some problems by for loop. For example 'json' parsing, 'xml' parsing etc. For this kind of problems you should use recursion. Some dynamic problems can be solved easily by recursion instead of for loop. For a hard problem you do not have to think much solving by recursion instead of for loop. You just form the Recurrence_relation.

Nayan
  • 151
  • 9
1

I prefer loop over recursive functions. See if this helps : Read it

Cijo V J
  • 261
  • 1
  • 3
  • 14