All recursive code can be translated into an iterative form, but some algorithms are naturally recursive and more easily represented in this way.
Consider, for example, traversing all nodes in a tree or graph, processing all files in a directory and subdirectories, and so on.
In iterative mode you are responsible for manually managing the state of each step of the algorithm using some data structure, often a stack, whereas in recursive mode you delegate this function to your programming language , by making use of variables and parameters that are automatically allocated in the execution stack at the beginning of each method method execution.
To answer the questions, consider something a little more complex. I extracted the pseudo-code to traverse trees from the Wikipedia .
if node == null then return
parentStack = empty stack
lastnodevisited = null
while (not parentStack.isEmpty() or node ≠ null)
if (node ≠ null)
node = node.left
peeknode = parentStack.peek()
if (peeknode.right ≠ null and lastnodevisited ≠ peeknode.right)
node = peeknode.right
lastnodevisited = parentStack.pop()
1st What is the advantage of using a Recursive Function?
In your example there is little advantage, but in what I put above, we see clearly that the recursive method:
Better expresses functionality . Scheduling works makes everything heavy of that capacity.
It's more compact . Less code means easier maintenance, less possibility of errors, and so on.
Automatic status management . In the iterative example a stack ( stack ) was used. Now think of languages where memory management is not automatic. In C, for example, you would have to add code to allocate and release stack elements at each iteration.
2nd How does the memory consumption?
Depends on the function. Generally you should do a memory and time complexity analysis to analyze the differences, but it is rather obvious that, in the vast majority of cases, recursive routines consume more memory and require more execution cycles.
Recursive routines often use more memory by relocating variables to each recursive call, but iterative routines can use the same amount of memory if values are saved in lists or stacks, for example.
In iterative mode you have control, for good or ill. It is possible to create a more optimized routine than the recursive version. But you should not take this as a rule, there are excellent recursive implementations that are more efficient than the average of iterative implementations.
In addition, efficiency depends greatly on the number of runs and the level of recursion. As well as sort routines, some implementations work best when there are few iterations and others when there are more iterations. Some recursive implementations are best for small processing. See, for example, on this site that performance for Fibonacci where
n <= 5 was better for the recursive version.
In a very simple example, the Fibonacci recursive function has exponential complexity while the version can be implemented with two or three simple variables and therefore has constant memory usage >.
On the other hand, recursive routines almost always need to be rewritten in their iterative version when the number of elements or numbers processed is large, after all the recursion has a sensitive limit of both memory and data stacking.
And beyond that, there are techniques that can improve the performance and capability of recursive routines, examples being:
3rd. Should I use a recursive function rather than a common Repetition Loop (eg for / while)? What would be the ideal situations for using recursive functions?
It should only if the algorithm is better expressed recursively and if the general use of that algorithm will not exceed the limits of recursion.
I believe that ideal situations have been exemplified above, ie:
- Inherently recursive algorithms, best expressed in this format
- Computational use is moderate, so as not to exceed the limits of recursion