# Iteration and recursion?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-18-2003
Munkey01
Iteration and recursion?
Could someone explain to me the difference in recursion and iteration? My book doesn't seem to explain what either really do(atleast I can't find it). Also if you don't mind can you give me examples of each?
• 01-18-2003
frenchfry164
recursion is when something calls itself. For example:

Code:

```void Function() {   Function(); }```
I don't know the exact meaning of iteration
• 01-18-2003
minesweeper
Iteration is a process of looking over a series of data. So you could say that a function that checks each element of an array in sequence is an iterative function. Same thing goes for if you write a function that reads over each node in a linked list in sequence.
• 01-18-2003
Munkey01
Quote:

Originally posted by frenchfry164
recursion is when something calls itself. For example:

Code:

```void Function() {   Function(); }```
I don't know the exact meaning of iteration

I thought it was considered bad when a function called itself :confused:
• 01-18-2003
It's not bad at all, it's often required. Recursively calling a function 10 times is slower than just calling the function 10 times without recursion.
• 01-18-2003
minesweeper
Well I guess if you didn't handle it properly it would just keep on going forever. But recursion i.e. functions calling themselves is a common technique in programming. You just make sure that with each call you have some code to check and see if a desired condition has been met by making the call. If it has, you 'bug out'.
• 01-18-2003
JoeSixpack
Quote:

It's not bad at all, it's often required. Recursively calling a function 10 times is slower than just calling the function 10 times without recursion.
At the lowest level. Using recursion at a higher level can be optimised out.
• 01-18-2003
Munkey01
Well most of the time instead of using recursion couldn't you use a while statement?
• 01-18-2003
orbitz
Most things can be written either way. Some recursive things cannot be written iterativly though. The usual example is the fabinocci sequence (spelling??). Do a google search and I'm sur eyou will find heaps of information.
• 01-18-2003
JoeSixpack
>Well most of the time instead of using recursion couldn't you use a while statement?<

Yes, do a search on tail recursion. But, some forms of recursion need a stack to be implemented in order to achieve the same functionality iteratively (i.e. quicksort).
• 01-18-2003
Munkey01
Quote:

Originally posted by orbitz
Most things can be written either way. Some recursive things cannot be written iterativly though. The usual example is the fabinocci sequence (spelling??). Do a google search and I'm sur eyou will find heaps of information.
A program that deals with fibonacci series can be written using recursion or iteration. Thanks for mentioning the fib's because I remembered reading something in my book about that and looked it up and there is the explination of iteration and recursion.
• 01-18-2003
orbitz
Hrmm. Yeah fib can be written iterativly, but to quote someone else:

Quote:

but it's definition hinges on subsequent elements being built from previous ones
Hence, recursion.
• 01-18-2003
JoeSixpack
>but it's definition hinges on subsequent elements being built from previous ones<

Is a for loop recursive?
• 01-18-2003
minesweeper
>>Is a for loop recursive?<<

No. I don't know but maybe you can think of the difference as being that a for loop performs a complete operation over and over again. Whereas recursion starts an operation, then starts the operation again, then again and doesn't finish any operations until the last has been started. So

Code:

```function() { function(); }```
function starts executing and then calls function to start executing again, and again etc.......Until a specified condition is met and then it will no longer call function. It will then begin returning from the functions in the opposite order to which they were called

any clearer, probably not :D That's recursion for you

EDIT: Looking over my explanation, it's not the best. To be honest I think the only way to actually understand recursion is to code a recursive method and actually see for yourself what is going on.
• 01-18-2003
JoeSixpack
Thanks for the awkward definition, minesweeper. My point was orbitz was talking ****.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last