DEV Community

Cover image for Reasons To Use Recursion and How It Works

Reasons To Use Recursion and How It Works

Manar Imad on August 27, 2021

Hello Engineers Today, I will talk about recursion in what, why, and how. What is recursion , why to use it, and how to solve problems w...
Collapse
 
joaomcteixeira profile image
João M.C. Teixeira

Nice post. Very interesting example with hello world being printed before the i value. It almost looks like a recursive decorator. ;-)

Recursion can be our best friend in many cases. But honestly, I am not sure that is best using recursion when iteration would work. I've run into some examples where recursion is needed and others where iteration is just best. What do you think?

I wrote once about Fibonacci with iteration and flat a list with recursion. Let me know your thoughts on it.

cheers

Collapse
 
manarabdelkarim profile image
Manar Imad

thank you .. I am glad you liked it 😊
and yes I agree with you 💯

looking at my example , the while function and the recursion function worked the same in result and time complexity .. but there is a difference that makes the while function better in this case which is the space complexity .
the while loop was updating the value of i which means the big O space complexity of the while function is O(1) "constant"
on the other hand, the recursion function was filling the stack every time with new function and different parameter , which means the big O space complexity is O(n)

so yes .. in many causes , using a normal iteration would be the best.

about your line in the article "I started this post to discuss that the Fib series might not be a good example to explain recursion." yes I totally agree and that's why I didn't use it
as you said there are better ways that will take less time and space

I liked your article I am gonna refer to it in my post

Collapse
 
joaomcteixeira profile image
João M.C. Teixeira

Thanks for the highlight 👏
and thanks for your comments. it is good to discuss. stay tuned. more is always coming 😉

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Because again, recursion functions use call stack and the call stack has a limit

I mean, that only applies to poorly designed languages, so it's not something to really worry about

EDIT: And poorly designed code, I should probably add

Collapse
 
manarabdelkarim profile image
Manar Imad

True 👍.. in many languages you can avoid this problem easily ..it seems like I will add writing a post about that to my future plans

Collapse
 
payalsasmal profile image
Payalsasmal

Hey 👋, it's really a nice article. I have enjoyed a lot. Thank you for sharing 😊

There is typo about LIFO for stack principal. It has written Last In Last Out instead of Last In First Out.

Collapse
 
manarabdelkarim profile image
Manar Imad • Edited

OMG thanks for telling me 😂 😂 😂
That's what happens when you write an article at midnight 😁

Collapse
 
payalsasmal profile image
Payalsasmal

Ha ha ha 😂. You are welcome.

Collapse
 
learner_25 profile image
Lion learner • Edited

That was informative and everything but I still hate recursive functions! Good job keep the good work

Collapse
 
manarabdelkarim profile image
Manar Imad • Edited

Thank you 🦁 .. I am trying to improve my content

Collapse
 
ohmammad profile image
OHMAMMAD

explained very well! it is still a very alian topic for me though...

Collapse
 
manarabdelkarim profile image
Manar Imad

Please tell me what is the alien thing about it .. I'm willing to write another article if it will help

Collapse
 
jonrandy profile image
Jon Randy 🎖️

You can avoid the stack becoming full for long running recursive functions using trampolining. Maybe a good follow up post 👍

Collapse
 
manarabdelkarim profile image
Manar Imad

Totally right 👍🏻👍🏻👍🏻

Collapse
 
aatmaj profile image
Aatmaj

You all might like this post too!

Collapse
 
manarabdelkarim profile image
Manar Imad

Good article 👍🏻
Keep it up

Collapse
 
bestape profile image
Kyle MacLean Smith • Edited

This dev-to-uploads.s3.amazonaws.com/up... was made by this JS-HTML recursion dapp: bestape.github.io/alchemy/

Collapse
 
manarabdelkarim profile image
Manar Imad

Are you the one who made it ?
that's so cool