# 14 Difference Between Recursion And Iteration With Example

SHARE

### What Is Recursion?

Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. The approach can be applied to many types of problems. Recursion is used in variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, where a function being defined is applied within its own definition.

### What Is Iteration?

Iteration is the repetition of a process in order to generate a sequence of outcomes. The sequence will approach some end point or end value. Each repetition of the process is a single iteration and the outcome of each iteration is then the starting point of the next iteration.

## Recursion Vs. Iteration In Tabular Form

 BASIS OF  COMPARISON RECURSION ITERATION Description Recursion involves a recursive function which calls itself repeatedly until a base condition is not reached (function that is partially defined by itself). Iteration involves the use of loops through which a set of statements are executed repeatedly until the condition is not false(loop based repetitions of a process). Code  Size Recursion keeps your code short and simple. Iterative approach makes your code longer. What it Entails In recursive function, only termination condition (base case) is specified. Iteration includes initialization, condition, and execution of statement within loop and update (increments and decrements) the control variable. Overhead Recursive functions involve extensive overhead as each function call the current state, parameters etc have to be pushed and popped out from stack. Overhead is absent in iteration. Structure Recursion uses selection structure. Iteration uses repetition structure. Termination Recursion terminates when a base case is recognized. Iteration terminates when the loop-continuation condition fails. Speed Due to overhead of maintaining stack, recursion is relatively slower than iteration. Iteration does not involve use of stack; therefore it’s relatively faster than recursion. Infinite condition Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition (base case). An infinite loop occurs with iteration if the loop-condition test never becomes false. Effect on Processor’s Operating Time Recursion increases the processor’s operating time. Iteration reduces the processor’s operating time. Memory Usage Recursion uses stack area to store the current state of the function due to which memory usage is relatively high. Iteration uses the permanent storage area only for the variables involved in its code block and therefore memory usage is relatively less. Time Complexity Very high (generally exponential) time complexity. Relatively lower time complexity (generally polynomial-logarithmic). Effect on CPU Infinite recursion can crash the CPU. Infinite looping uses CPU cycles repeatedly. Application Factorial, Fibonacci series etc. Finding average of a data series, creating multiplication table etc.
SHARE