4

What is the difference? Are these the same? If not, can someone please give me an example?

MW: Iteration - 1 : the action or a process of iterating or repeating: as a : a procedure in which repetition of a sequence of operations yields results successively closer to a desired result b : the repetition of a sequence of computer instructions a specified number of times or until a condition is met

Recursion - 3 : a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first

oguz ismail
  • 1
  • 16
  • 47
  • 69
Strawberry
  • 66,024
  • 56
  • 149
  • 197
  • The difference is recursion gives you a stack without having to ask for one. Iteration makes you ask for one if you need one. Everything else is how weird it looks. – candied_orange May 27 '17 at 04:42

7 Answers7

10

We can distinguish (as is done in SICP) recursive and iterative procedures from recursive and iterative processes. The former are as your definition describes, where recursion is basically the same as mathematical recursion: a recursive procedure is defined in terms of itself. An iterative procedure repeats a block of code with a loop statement. A recursive process, however, is one that takes non-constant (e.g. O(n) or O(lg(n)) space) to execute, while an iterative process takes O(1) (constant) space.

For mathematical examples, the Fibonacci numbers are defined recursively:

Fibonacci function

Sigma notation is analogous to iteration:

harmonic number

as is Pi notation. Similar to how some (mathematical) recursive formulae can be rewritten as iterative ones, some (but not all) recursive processes have iterative equivalents. All recursive procedures can be transformed into iterative ones by keeping track of partial results in your own data structure, rather than using the function call stack.

Community
  • 1
  • 1
outis
  • 75,655
  • 22
  • 151
  • 221
  • I like most of this answer, but I disagree that recursion implies being non-constant space. http://en.literateprograms.org/Fibonacci_numbers_%28Scheme%29 Compare the "direct" and tail-recursive approaches. By the above definition, the tail-recursive function in the link is not recursive :-/ –  Apr 05 '10 at 23:34
  • 2
    @pst: I think you've missed the point. A function that uses tail recursion is a recursive procedure and generates an iterative process. Recursive processes use non-constant space by definition. – outis Apr 06 '10 at 01:23
  • @outis: Don't get you quite well... First you say r&i PROCEDURES are different from r&i PROCESSES. one is constant in space, other isn't. Got that. Then you say iterative procedure loops, while iterative process is constant in space. What is the difference between i PROCEDURE and i PROCESS? Loops can be constant but don't have to. – CoR Jun 07 '12 at 23:39
  • 1
    @CoR: read my answer again, and more closely. The difference in space usage is the difference between recursive & iterative processes, not the difference between procedures and processes (space & time complexity is here a property of processes, not procedures). The difference between an iterative procedure and iterative process is basically the same as the difference between a procedure and a process, with the added caveat that the definition of the term "iterative" in each context is at most indirectly related (a recursive procedure can generate an iterative process and vice-versa). – outis Jun 08 '12 at 01:06
2

As per the definitions you mentioned, these 2 are very different. In iteration, there is no self-calling, but in recursion, a function calls itself

For example. Iterative algorithm for factorial calculation

fact=1
For count=1 to n
fact=fact*count
end for

And the recursive version

function factorial(n)
if (n==1) return 1
else
n=n*factorial(n-1)
end if
end function

Generally

Recursive code is more succinct but uses a larger amount of memory. Sometimes recursion can be converted to iterations using dynamic programming.

Midhat
  • 17,454
  • 22
  • 87
  • 114
2

[Hurry and trump this!]

One form can be converted to the other, with one notable restriction: many "popular" languages (C/Java/normal Python) do not support TCO/TCE (tail-call-optimization/tail-call-elimination) and thus using recursion will 'add extra data to the stack' each time a method calls itself recursively.

So in C and Java, iteration is idiomatic, whereas in Scheme or Haskell, recursion is idiomatic.

1

Here's a Lisp function for finding the length of a list. It is recursive:

(defun recursive-list-length (L)
  "A recursive implementation of list-length."
  (if (null L)
      0
    (1+ (recursive-list-length (rest L)))))

It reads "the length of a list is either 0 if that list is empty, or 1 plus the length of the sub-list starting with the second element).

And this is an implementation of strlen - the C function finding the length of a nul-terminated char* string. It is iterative:

size_t strlen(const char *s)
{
    size_t n;

    n = 0;
    while (*s++)
        n++;
    return(n);
}

You goal is to repeat some operation. Using iteration, you employ an explicit loop (like the while loop in the strlen code). Using recursion, your function calls itself with (usually) a smaller argument, and so on until a boundary condition (null L in the code above) is met. This also repeats the operation, but without an explicit loop.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
1

Recursion: Eg: Take fibonacci series for example. to get any fibonacci number we have to know the previous one. So u will berform the operation (same one) on every number lesser than the given and each of this inturn calling the same method.

fib(5) = Fib (4) + 5

fib(4) = Fib (3) + 4 . . i.e reusing the method fib

Iteration is looping like you add 1+1+1+1+1(iteratively adding) to get 5 or 3*3*3*3*3 (iteratively multiplying)to get 3^5.

Aadishri
  • 1,341
  • 2
  • 18
  • 26
0

For a good example of the above, consider recursive v. iterative procedures for depth-first search. It can be done using language features via recursive function calls, or in an iterative loop using a stack, but the process is inherently recursive.

0

For difference between recursive vs non-recursive; recursive implementations are a bit easier to verify for correctness; non- recursive implementations are a bit more efficient.

Algorithms (4th Edition)

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158