Whats the difference between recursion and loops? Is there any speed difference between them or not ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Whats the difference between recursion and loops? Is there any speed difference between them or not ??

6th May 2020, 2:57 PM
Abhay Kalsi
Abhay Kalsi - avatar
4 Answers
+ 3
Yes, absolutely...that's why you still can use it. But the risk remains because every time you call a rec function it occupies a newer part of the stack, while in loops at every iteration data is overloaded. It depends on how you write your code, you just have to be careful when using rec.
6th May 2020, 3:22 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
+ 2
But sir Stack overflow can be prevented using conditionals
6th May 2020, 3:16 PM
Abhay Kalsi
Abhay Kalsi - avatar
0
Generally it's safer using loops...recursion can generate stack-overflow. Sometimes you can use recursion to make the code easier to read and for problems that can be broken into same smaller problems but you have to make sure that stack-overflow won't occur.
6th May 2020, 3:11 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
0
Recursion means calling a function to itself. Loop means executing the code certain amount of time. ( Here we already knows how many time the code get executed)
6th May 2020, 3:24 PM
Dasarath Singh