Thread: recursive function

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    recursive function

    how can we convert an iterative function into recursive function ..

    i know few recursive functions but every time i am asked to convert an iterative function into a recursive one ...i just get perplexed...

    how do we really go for a recursive function ...i mean is there any particular way that i can try cracking an iterative function into its corresponding recursive version..

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    You don't convert iterative functions to recusive functions. When you think what is a recursive function, think also what kind of written code it could be used to replace. Recursive conversion, is possible on loops (while, for, do - while, if goto, etc).
    Code:
    void countdown(int start)
    {
        int n = start;
        while(n>0)
        {
            printf("Countdown to %d\n", n);
            n--;
        }
    }
    The example function presented can be converted to a recursive.
    Code:
    void countdown(int n)
    {
        if(n==0)
            return;  /* Break recursion */
    
        printf("Countdown to %d\n", n);
        countdown(n-1); 
    }
    Of course this is a simple example but the same principle applies to most recursive transformations.
    It might interest you to know, that the task of converting recursive to non-recursive functions, is actually of much more use, and interest.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM