Recursion Vs Looping

I recently heard from my friend that recursion is more efficient than the normal method of iteration.
But being a newbie with my limited knowledge i cant believe that. Is that the real fact?
Can any one please help me out?

1 Like

@bipin2 Recursion is easy to implement but Looping way (Iterative Approach) is always better to use.

There are several reasons 1 is Recursion may cause Stack Overflow problem If you have not implemented it correctly , Forgot to use base cases and termination cases for recursion code.
And more , it is very difficult to debug if you have several Recursive calls in you code.

So always prefer iterative approach while coding … for some algorithm iterative is also difficult to implement so we can’t say exactly that which is better…

1 Like

Hi,

Iteration is generally more efficient except in the cases of functional programming languages such as LISP.
But undoubtedly, most of the problems are easier to solve by recursion, hence recursion techniques are used mostly where efficiency is a secondary issue.

1 Like

Successive recursive function calls use stack to execute them.

In the case of recursion, all the recursive function calls are stored on the stack to return control back to the caller functions poping the recursive calls, therefore making recursion slower than looping, because of these overheads.

But there are some algorithms that will be very difficult to implement if not done recursively, like many tree traversal algorithms, graph traversals like dfs, bfs, etc.

So, the bottom line is that recursion makes problem solving easy in many complex algorithms, which makes the overheads involved in recursive function call not that important when comparing them with the complexity of the problem itself.

1 Like

thanks bro!!!