Recursion: A general question

Recursion is there to remove iterative loops. But using a for loop beats the advantage of it. Is this true?

recursion take more space because of call stack. but recursion has more advantages you will learn lot of concept in upcoming course where you will not able to solve the problem without recursion. and loop simple iterate doesn’t take more memory then recursion and sometime it is faster than recursion

There are certain pros and cons of using recursion.
Advantages:

  • Recursion adds clarity and (sometimes) reduces the time needed to write and debug code (but doesn’t necessarily reduce space requirements or speed of execution).
  • Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex.
  • Reduces time complexity.

Disadvantages:

  • Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC.
1 Like

No, actually I wanted to know that we can obviously use for loop inside recursive function but isn’t its job to remove the iterative methods? so how correct is using loops inside recursive function?

Hi Hardik, although recursion can be used to replace iteration, there is absolutely no requirement for the recursive function not to have iterations internal to itself. The only requirement for a function to be considered recursive is the existence of a code path through which it calls itself, directly or indirectly. However you should have some condition to prevent infinite recursion calls from which your function can’t return. You are right to think there is a relationship between recursion and iteration or looping. Recursive algorithms are often manually or even automatically converted to iterative solutions using tail call optimization.

1 Like

Thank you! This resolved my question.