# Thread: Iteration and recursion?

1. ## 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?

2. recursion is when something calls itself. For example:

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

3. 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.

4. 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

5. 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.

6. 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'.

7. 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.

8. Well most of the time instead of using recursion couldn't you use a while statement?

9. 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.

10. >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).

11. 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.

12. Hrmm. Yeah fib can be written iterativly, but to quote someone else:

but it's definition hinges on subsequent elements being built from previous ones
Hence, recursion.

13. >but it's definition hinges on subsequent elements being built from previous ones<

Is a for loop recursive?

14. >>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 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.

15. Thanks for the awkward definition, minesweeper. My point was orbitz was talking ****.

Popular pages Recent additions