answersLogoWhite

0


Best Answer

In computer programming, both iteration and recursion define a type of loop. With iteration, the loop makes use of the current instance of the function in which it appears. When we start a new iteration, the "state" of the previous iteration is carried forward to the new iteration but we cannot return to a previous state. With recursion we can return to a previous state because each recursion invokes a new instance of the function which automatically saves the state of the current instance. Whenever we return from an instance, the result of that instance can be passed back to the previous instance and be incorporated into its restored state.

To demonstrate, consider the following iterative loop (in C++):


for (unsigned i=0; i<=10; ++i)

std:cout << i << std::endl;


Here we print the numbers from 0 to 10. Each iteration of the loop is not reliant upon the previous iteration so recursion is not required. However, we can achieve the same thing using recursion:


void print (unsigned max)

{

if (max!=0) print (max-1);

std::cout << max << std::endl;

}


Here we test the value of max. If it is non-zero we invoke a new instance of the same function, recursively passing the decremented value of max. Thus if max were 10, we'd invoke print (9), which invokes print (8), then print (7) and so on, until print (0) is invoked. At this point there are 11 instances of the print function, thus the "depth of recursion" is 11 and each instance of the function has its own instance of the max variable. Since max is zero in the 11th instance, the condition fails and the next statement is executed, which prints the value 0 and returns to the previous instance where max was 1. In that instance we'd already tested if max were zero so we continue with the next statement which prints the value 1 and returns to the previous instance where max was 2. The recursions continue to "unwind", each printing their own max value until we arrive back at the first instance where max was 10 and the value 10 is printed.


This is not a good example of recursion, but it clearly demonstrates how recursion works. In essence, the recursive loop works backwards from 10 to 0, then unwinds to print from 0 to 10.


There are many examples of naturally recursive algorithms that are more efficiently implemented using iterative loops. The reason they are more efficient is because whenever we invoke a function we must save the state of the current function, which means pushing all the local variables onto the call stack. We also have to push the return address onto the call stack so that the called function can pass control back to the current function at the correct place. Finally, we have to push all the arguments to the called function onto the stack before we pass control to that function. Upon entering the called function, the arguments are popped from the stack and it can begin processing. When the function returns, the return address is popped from the stack and control passed to that address. When the caller regains control, its local variables are popped from the stack. If the called function returned a value, it is retrieved from the appropriate CPU register and assigned to the appropriate variable.


This is a lot of work, which is why C++ compilers make use of inline expansion, replacing function calls with the actual code in the function. However, not all functions can be inline expanded. Recursive functions are particularly problematic when the depth of recursion is variable but even when a particular invocation is constant, the compiler may limit the number of expansions because the efficiency gained by eliminating each function call needs to be balanced with the increased code size, which can be counter-productive if the function is non-trivial.


However, there are cases where recursion is more efficient than an iterative solution. Our example above is an example of depth-first recursion which is a naturally recursive algorithm, where we dig down to reach an exit condition and then work our way back up. For instance, to traverse the nodes of a binary tree using depth-first search, we initiate the traversal by passing the root node to the traversal function. This function performs three tasks upon the node it receives: first traverse the node's left node; then print the node's value; then traverse the node's right node. In other words, the function recursively invokes itself twice. Writing such a function iteratively is simply not possible because the depth of each recursion can vary so wildly.


This type of function uses a "divide and conquer" algorithm and in cases where the depth of recursion is unpredictable (non-constant) recursion is often the most efficient choice because the call stack serves a purpose that cannot easily be implemented with an iterative approach. However, because such functions often make use of tail recursion (where the final statement of the function is a recursion), the call stack becomes redundant because the current instance of the function would simply return immediately after that recursion. In these cases, a good compiler can modify the function such that the tail recursion becomes an iteration of the same instance of the function; rather than invoking a new instance it simply re-invokes the same instance with modified arguments. Going back to our traversal function, once the function has printed the current node's value, we no longer need to remember the current node, we can simply assign its right node as the current node and go back to the beginning of the same instance of the function. This is much more efficient because we only perform one assignment as opposed to going to the expense of a function call, which results in a minimum of three pushes and three pops from the call stack.


User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Iterations are the looping conditions which continues till a particular condition is met whereas the recursions are related to calling the function itself repeatedly

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the differences between iteration and recursion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Bring out the difference between recursion and iteration?

Recursion is repeatedly calling the function....---- whereas Iteration is nothing but just looping until condition doesn't satisfy.....


What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What is non recursion called?

Iteration. A problem not solved recursively is solved iteratively.


Which is faster to find factorial whether recursive or dowhile?

recursion is always slower than iteration


Is newton rephson and successive bisection recursion or iteration?

They are iterative methods, but they can be implemented as recursive methods.


Which one is advantageous recrsion or iteration?

Depends... I teach algorithms and advice my students to choose whichever they find simpler to implement. Sometimes recursion is more intuitive than iteration and viceversa. All that is recursive can be done iterative and the other way around. The only problem you would have with recursion is having a stack overflow (if you call the recursive method too many times).


What is iteration in c programming?

Iteration means what it says: the act of repeating a process. Each repetition of the process is itself an iteration, and the results of one iteration can be applied in the next iteration. Counting from 1 to 10 is an example of an iterative process because each iteration increments the counter by 1. Iteration should not be confused with recursion. Although similar, a recursion occurs when a function calls itself. Such functions are known as recursive functions and these make use of the call stack to remember the "state" of each function prior to the next call, thus allowing those states to be restored when the recursive calls return (known as "unwinding"). Since the call stack must be maintained regardless of the depth of calls, recursive routines that do not need to remember the state of each recursion are inefficient, and are often better implemented as iterative loops. However, this may require nested iterations and, if the depth is too variable or complex to be calculated at compile time, or the maximum depth would be greater than 16 then the cost of recursion will often be preferred to the increased code size iteration would incur. Even so, recursive functions can often be inline expanded by the compiler as iterative functions, thus simplifying the source code without sacrificing the performance.


What are the merits and demerits of recursion?

Ans: Merits of recursion are: Mathematical functions, such as Fibonacci series generation can be easily implemented using recursion as compared to iteration technique. Demerits of recursion are: Many programming languages do not support recursion; hence, recursive mathematical function is implemented using iterative methods. Even though mathematical functions can be easily implemented using recursion, it is always at the cost of execution time and memory space. The recursive programs take considerably more storage and take more time during processing.


How do you choose between recursion and iteration?

Some problems cry out for recursion. For example, an algorithm might be defined recursively (e.g. the Fibonacci function). When an algorithm is given with a recursive definition, the recursive implementation is straight-forward. However, it can be shown that all recursive implementations have an iterative functional equivalent, and vice versa. Systems requiring maximum processing speed, or requiring execution within very limited resources (for example, limited stack depth), are generally better implemented using iteration.


What are the differences between iterations and loops in progamming?

A loop will loop for n iterations. Each times the program executes the code in the loop is an iteration.


What is the difference between looping and recursion?

Both Iteration and Recursion run a part of an algorithm repeatedly. We typically use them when, in a part of an algorithm, we have to do or calculate something (i,e. the logic remains the same though the data changes) for a certain number of times. However,in Recursion, we simply run a loop for a certain number of times in the algorithm, such as printing a statement ten times is recursion. In Iteration, we specifically run a loop, such that, the second run of the loop makes use of the computation/result from the first run, the third run (or iteration) makes use of the result from the second run and so on. Hence, in Iteration, the n th run of the loop makes use of the result from the n-1 th run. Consider the following example for calculating the summation, which we will denote as sum(n), meaning n + n-1 + n-2 + n-3 + ... + 2 + 1 + 0. Hence, sum(5) = 5+4+3+2+1+0 = 15. Now we will write psuedo-code to calculate the sum(n) using Iteration and Recursion. Iterative Version:- ----------------- start sum(n) { i = 0; total = 0; do while i is not greater than n, { total = total + i; i = i + 1; } return total; } Recursive Version:- ----------------- start sum(n) { if n is equal to 0, then return(0); otherwise, return (n + sum(n - 1)); } Notice that the Iterative version calculates the sum by repeatedly accumulating the sum in the variable total. In contrast, the recursive version simply repeatedly calls itself, each time with a decremented value. .


How do you overcome limitations of stacks in polygon filling?

You overcome limitations of the stack in polygon filling, or in any other algorithm, far that matter, but using an iterative technique, rather than a recursive technique. Recursion is quite useful, and can simplify algorithm design. Polygon filling, however, is a class of algorithm can potentially have a very deep recursion depth. This causes stress on the stack, hence the need for iteration.