Recursive vs non-recursive [duplicate]

戏子无情 提交于 2019-12-13 08:08:50

问题


Possible Duplicate:
Recursion and Iteration

What is the difference between a recursive and a non-recursive function? Fibonacci to be exact.

I looking for answers that relate towards the time and memory.


回答1:


"Recursive" simply means that a function calls itself. This may or may not be intentional (unintentional recursion is responsible for lots of crashes).

Intentional recursion, where a function performs part of an operation, then calls itself to perform the remaining part, is often a useful programming paradigm, but requires some degree of comprehension/experience/skill to "get your head around it".

Basically, recursion can be used to replace "iteration" (loops) and to replace accompanying array allocations (with variables local to the function body). But not every iterative or array-using function can be effectively converted to its recursive equivalent.

If the problem is suitable for recursion, one can often write a recursive version that is about equivalent in execution efficiency to the non-recursive version ... maybe slightly better or worse depending on how efficient the call mechanism is compared to looping and array indexing in the language/compiler. In terms of storage, recursion is rarely more efficient, but it benefits from not having to pre-allocate (and pre-know the size of the allocation) for the particular problem at hand.

Mostly recursion is better (when it actually is) because it makes an implementation much simpler and less error-prone, and errors are by far the biggest cost in computing. (But of course improperly done it can cost you big time as well.)

When recursion is good it's very good. When recursion is bad it's very bad.




回答2:


Recursive functions are procedures or subroutines implemented in a programming language, whose implementation references itself.

Non Recursive Function are procedures or subroutines implemented in a programming language, whose implementation does not references itself

Below is a link for recursive and non recursive fibonacci series:- Recursive and Non Recursive Fibonacci Series



来源:https://stackoverflow.com/questions/12939126/recursive-vs-non-recursive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!