Why recursive functions are bad




















Once, you get an idea of how recursion works and which base case to work with, it runs smooth as ice. For all those people who want to make their code look pretty as a picture, recursion is the best way to do that. Recursion makes a code more compact, readable and so very elegant. If you can master recursion, it can turn out to be your best friend. Solutions using recursion are easier to strike and code. It also avoids redundancy of code, and your codes are easier to read and maintain.

Recursion can lead to the perils of stack overflow. Mostly, all of these steps consume more time than their iterative counterparts. Therefore, recursive methods are relatively less efficient in those cases. More importantly, when most programs start, they allocate a single chunk of memory to the stack, if that memory is used, the entire program crashes due to stack overflow. Each function call eats up a lot of space. Iterative methods do not suffer from such problems. How do you code recursion?

What is recursion with an example? What is recursion in language? Active 2 years, 5 months ago. Viewed 17k times. Improve this question. Community Bot 1 1 1 silver badge. Neinstein Neinstein 1 1 gold badge 7 7 silver badges 29 29 bronze badges.

If this better suits for Theoretical Computer Science, feel free to migrate. Not really opinion based for some languages. When the toolchain of choice or perhaps VM doesn't support TCO simple recursive functions should be implemented by other control structures. For calls that cannot be in tail position it's a matter of predictable depth like a O log n space or predictable small data structure so that a concise recursive function can be used and then preferred rather than a solution that backtracks with a utility stack on the heap that does not grow the system stack which would be less readable but more robust.

Recursion is bad when you need to process a big amount of data tree with deep for example and can lead to stack overflow exception — dorintufar. Add a comment. Active Oldest Votes. Improve this answer. Bertram Gilfoyle 8, 5 5 gold badges 36 36 silver badges 62 62 bronze badges. Rick the Scapegoat Rick the Scapegoat 7 7 silver badges 19 19 bronze badges. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Having a discussion about the plusses and minuses can be valuable, but sometimes what you need is a motivating example.

It's true that loops and recursion are equivalent in the sense that they can provably accomplish the same tasks. However, some problems are simply astoundingly easier with recursion, such as iterating through a tree, or finding all of the anagrams of a set of letters.

I mention this second example because it can be brought up in a introductory class long before trees come into the picture. In addition to the other good answers already provided, I want to add that students should learn recursion because they will encounter it in real life.

Any one of them who goes on to program for a living will at some point need to understand, maintain, or debug a recursive function. So they must be able to recognize recursion, understand how it works, see when it's broken, and know how to fix it.

It is generally agreed that looping is better than recursing - when you can actually get the job done with a loop. But there are times when it is far easier to crack the nut with recursion rather than a loop. And as a programmer, you must be armed for those times. An introductory programming class is a good place to familiarize students with some real-world issues in programming apart from the more popular ones. Using recursion is just one example of how they might address them.

Programming for the long haul whether it's for production or to support one's own research, for example is not always about obtaining the best performance in terms of computer resource use.

Recursion is just one example of how to overcome a programming obstacle in order to satisfy an immediate objective. In cases where the most intuitive understanding of a problem is in terms of recursion, then there are reasons to use recursion:. So recursion becomes another tool that helps a programmer get where he needs to be in terms of function and maintainability. Once those two factors have been addressed, then, if time and budget permits, then the software can be optimized in other areas hopefully why still being functional AND maintainable.

So you can implement while True: recursively. In these languages recursion is used more. In python it is used less, unless the depth is as you said O log n. However the design may be easier done using recursion, and you can always convert recursion into iteration. When teaching students who are old enough or invested enough to care about performance the younger ones typically do not , I introduce it like this:.

You choose. If you're feeling lazy, and performance isn't an issue, recursion is perfect: you will finish writing it sooner, and you'll have fewer bugs. If you're working on a performance-critical problem, use loops: they are much faster and more efficient for the computer, and although it'll take you more effort to write, it might make the difference between "fast enough" and "too slow to be useful"".

The practical interest of recursion is obvious to the students when they are presented with problems where the underlying data structure is, for example, tree. For exemple : figures that can be circles, squares, or group of figures. Or directories containing files and directories. XML nodes, etc. Recursive descent to evaluate an expression They don't even ask how to process them by iterative programs.

Obviously good and best practice. The classical examples about numbers are counterproductive : factorial, fibonacci, etc.

So why should they spend efforts learning how to do it painfully a "less natural" way? For recursion, the second most important detail is to have an exit statement. That is the condition upon which the recursion terminates. So long as the exit statement is good, recursion is a very, very powerful tool. This has a well defined exit statement, that will be met, because every call decreases position by 1, and the exit checks if position is 0.

Which eventually happens for any positive integer value being decremented. This shows that recursion is very useful, BUT it must have a good exit statement. So saying "it's horrible in any programming language" is very misleading.

So in essence, the main point is showing how important choosing a good exit statement is. A professional programmer could go most of her or his career without having to resort to it.

These tend to be rare. Cons, very prone to error, the algorithms may not be particularly intuitive and in most cases can be replaced with an iterative approach.

Besides Flood Fill, the recursive solution to the Tower of Hanoi problem is another powerful illustration of how powerful a recursive algorithm can be. Most things in programming should be thought of as tools. Languages are tools.

Algorithms are tools. Various approaches are tools. Different tools are for different jobs. A carpenter learns about a hammer, a saw, a wrench, etc because sometimes the hammer will be the best tool for the job, sometimes it'll be the saw, and sometimes it'll be the wrench. Sometimes, the best tool will be something the carpenter has never used before!

But the more tools they already know, the easier it'll be to learn about new tools. Having learned about a wrench makes it easier to learn about a screwdriver when you need to. So learning about a tool is never wasted time: even if we don't use the tool every day, it helps us learn about stuff we need to know. That's why we learn about a bunch of different things in programming.

Sometimes a loop will be the best tool.



0コメント

  • 1000 / 1000