Can someone please differentiate between iteration and recursion. Both are looking same to me..I know there will be a difference but don't know what . Please help me know the difference
3 Answers
Recursion is when a function/method is called from within the same function/method (directly or indirectly). This results in each successive call having a copy of its local variables on the stack (or wherever), and it needs to be 'unwound' at the end, by ending each of the functions/methods and coming back to the previous call. Recursion often result in relatively short code, but use more memory when running (because all call levels accumulate on the stack)
Iteration is when the same code is executed multiple times, with changed values of some variables, maybe better approximations or whatever else. An iteration happens inside one level of function/method call and needs no unwinding.

- 6,295
- 1
- 12
- 23
I hope this article will explain you: http://www2.hawaii.edu/~tp_200/lectureNotes/recursion.htm
Explain in pseudo code:
recursion
function f(x){
do y;
if(x<0){ return f(x-1) } else { return }
}
iteration
for(x in 1 to 10){
do y;
}

- 766
- 7
- 19
You iterate by repeating a function.
Example, i is the iterator:
for (i = 0; i < 10; i++){
function(input);
}
You recurse by using the function within itself.
Example:
function(input){
if (input == outcome) {return;}
else {function(input+1);}
}

- 11
- 2