## Why we need recursion?

recursion c++

recursion in c

recursion examples

recursive function

recursion in python

how to do recursion

application of recursion

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) { } }

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*.

**Why we need recursion?,** 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. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily.

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.

**Recursion,** We return 1 when n = 0. Why Stack Overflow error occurs in recursion? If the base case is not reached or not defined, then the stack overflow problem may arise. Depends on compiler I think. Recursion is a paramount of functional programming. [ UD: Please adhere to the "Be Nice" rule. The overhead becomes sizeable when the problem is tree-recursive (i.e., involves more than one recursive call per step) and goes to great depths. I think that is what i am trying to do here.

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

**What is the function of recursion? Why do we need recursion in ,** Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself. For example, we can define the operation The recursive function is most efficient and logical as a programming tool when the static data is the functional state as in the branching algorithm above, Fibonacci sequences or most Chess programs.

**What is recursion and when should I use it?,** Recursion is associated strongly with functional programming. We do use recursion more than imperative programmers do. But we also use Recursion can help to simplify the implementation of some complicated problems by making the code clearer and more readable. But as we've already seen the recursive approach often requires more memory as the stack memory required increases with each recursive call.

**Recursion (computer science),** In computer science, recursion is a method of solving a problem where the solution depends on Multiple recursion, by contrast, may require exponential time and space, and is more fundamentally recursive, not being able to For that reason, we refer to these functions as (STRUCTURALLY) RECURSIVE FUNCTIONS.

**Reading 10: Recursion,** Recursion. In today's class, we're going to talk about how to implement a To finish the implementation, we need to implement the original subsequences spec,

##### Comments

- The answer is you don't need recursion for a simple example like this.
- Then when recursion is need. can u give me examples ???
- lol Why do you have infinite loops at the end of both the programs?
- 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: summarizing, "recursion exists so students can be taught recursion" 😄😄
- + 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.
- Thanks @Carcigenicate