# Difference between Recursion and Iteration | Iteration vs Recursion

Written by

Pooja Rao

Recursion and Iteration are both used for executing a set of statements repeatedly, until the condition becomes false.

## Recursion

Recursion is applied to functions, where the function calls itself to repeat the lines of code/ certain statements.

## Iteration

Iteration, on the other hand, uses looping in order to execute a set of statements multiple times. A given problem can be solved both by recursion as well as iteration, however, they have certain important differences as well.

## Difference between recursion and iteration

 Parameter Recursion Iteration Definition Recursion involves a recursive function which calls itself repeatedly until a base condition is not reached. Iteration involves the usage of loops through which a set of statements are executed repeatedly until the condition is not false. Termination condition Here termination condition is a base case defined within the recursive function. Termination condition is the condition specified in the definition of the loop. Infinite Case If base case is never reached it leads to infinite recursion leading to memory crash. If condition is never false, it leads to infinite iteration with computers CPU cycle being used repeatedly. Memory Usage Recursion uses stack area to store the current state of the function,due to which memory usage is high. Iteration uses the permanent storage area only for the variables involved in its code block, hence memory usage is less. Code Size Code size is comparitively smaller. Code size is comparitively larger. Performance Since stack are is used to store and restore the state of recursive function after every function call , performance is comparitively slow. Since iteration does not have to keep re-initializing its component variables and neither has to store function states, the performance is fast. Memory Runout There is a possibility of running out of memory, since for each function call stack area gets used. There is no possibility of running out of memory as stack area is not used. Overhead Recursive functions involve extensive overhead, as for each function call the current state, parameters etc have to be pushed and popped out from stack. There is no overhead in Iteration. Applications Factorial , Fibonacci Series etc. Finding average of a data series,  creating multiplication table etc. Example #include int fact(int n){ if(n == 0) return 1; else return n * factorial(n-1); } int main() { printf(“Factorial for 5 is %d”, fact(5)); return 0; } Output:  Factorial for 5 is 120 #include int main() { int i, n = 5, fact = 1; for(i = 1; i <= n; ++i) fact = fact * i;printf(“Factorial for 5 is %d”, fact);return 0; }Output:  Factorial for 5 is 120