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?
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?
recursion is when something calls itself. For example:
I don't know the exact meaning of iterationCode:void Function() { Function(); }
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.
I thought it was considered bad when a function called itselfOriginally posted by frenchfry164
recursion is when something calls itself. For example:
I don't know the exact meaning of iterationCode:void Function() { Function(); }
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.
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'.
At the lowest level. Using recursion at a higher level can be optimised out.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.
Joe
Well most of the time instead of using recursion couldn't you use a while statement?
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.
>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).
Joe
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.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.
Hrmm. Yeah fib can be written iterativly, but to quote someone else:
Hence, recursion.but it's definition hinges on subsequent elements being built from previous ones
>but it's definition hinges on subsequent elements being built from previous ones<
Is a for loop recursive?
Joe
>>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
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 calledCode:function() { function(); }
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.
Last edited by minesweeper; 01-18-2003 at 08:08 PM.
Thanks for the awkward definition, minesweeper. My point was orbitz was talking ****.
Joe