Savings memory | Sololearn: Learn to code for FREE!


Savings memory

Does recursion sometimes saves more memory usage than normal repeat method ?

11/22/2017 7:42:06 PM

Silver Alex Xatsatourian

3 Answers

New Answer


It depends on what the problem is that is needing to be solved, and the skill of the said programmer. That is why the concept still exists alongside loops. Some problems can be solved more efficiently by recursively calling a function. Recursion is generally more clean, and less complicated ... depending on the problem of course. The basic concept is you write a function with a base case that acts as the final solution, and several other cases that break up the problem and pass them to itself again (imagine adding a new worker to help). Once the problem has been solved (conditions met), the base case returns its solution back up the ladder while collecting the smaller pieces along the way. This is a powerful concept, and also a complex one to use in complicated problems. Many programmers fall victim to the phrase "Looking into the rabbit hole". It's very hard to ignore and avoid sometimes. Because in programming you inherently need to be precise and know what's going on for the most part. But in this case, doing so will cause you to make mistakes and try to predict something you can't observe. So many programmers go for using loops instead. Loops are faster than recursion, sometimes. Loops can solve a problem faster, sometimes. But other times, where a loop requires several lines of code (and perhaps more loops), recursion can do with just the given original function. Recursion has the danger of having stackoverflow occur when too many calls are issued. Basically, imagine a stack of plates, and the limit is 100 plates. The first plate you have is your original function call, and it calls itself again to break up the problem, adding another plate to the stack. This repeats until you hit the limit, and the plates come crashing down ... or in a computer's case (Stackoverflow). Loops and recursion have their place in programming. A lot of programmers dismiss recursion and go for the former out of convenience and less difficulty. But only experienced programmers can truly appreciate them.


*ran out of characters, WALL OF TEXT!* Read this post on StackOverFlow (see where that website name comes from?): Its one of my favourite explanations of the differences.


Thanks a lot mate . Your answer is the best so far that I heard