Thread: Iteration and recursion?

  1. #1

    Question 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. #2
    recursion is when something calls itself. For example:

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

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    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. #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. #5
    Shadow12345
    Guest
    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. #6
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    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. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    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.
    Joe

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

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    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. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >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

  11. #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. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    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. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >but it's definition hinges on subsequent elements being built from previous ones<

    Is a for loop recursive?
    Joe

  14. #14
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    >>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.
    Last edited by minesweeper; 01-18-2003 at 08:08 PM.

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Thanks for the awkward definition, minesweeper. My point was orbitz was talking ****.
    Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iteration and recursion question
    By Stonehambey in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2008, 06:16 PM
  2. Recursive function crashes.
    By samus250 in forum C Programming
    Replies: 9
    Last Post: 01-16-2008, 02:58 PM
  3. Recursion vs. Iteration
    By simpleid in forum C++ Programming
    Replies: 5
    Last Post: 06-06-2007, 01:17 PM
  4. What is the difference between Recursion and Iteration?
    By neo_phyte in forum C Programming
    Replies: 12
    Last Post: 09-13-2006, 02:13 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM