Thread: Recursion woes...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    43

    Recursion woes...

    These are 2 slightly different versions of a recursive program for factorial :-

    Code:
    int fact(int x)
    {
     if(x == 1)
      return 1;
     else
      return x * fact(x--); 
    }
    Code:
    int fact(int x)
    {
     if(x == 1)
      return 1;
     else
      return x * fact(--x);
    }
    The first one results in a stack overflow with the recursive calls being :-
    (for say, x = 5)
    rec(5)
    rec(4)
    rec(4)
    rec(4)...

    The second one gives an output neglecting the starting value of x.

    Can anyone shed some light please ? Thanks.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Postfix decrement returns the x before the subtraction, prefix decrement does not.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    43
    Quote Originally Posted by whiteflags View Post
    Postfix decrement returns the x before the subtraction, prefix decrement does not.
    I still don't get it

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    AS whiteflags said, there is a difference between pre and post decrement.

    Because of that difference, your first is (roughly) functionally equivalent to
    Code:
    int fact(int x)
    {
     if(x == 1)
      return 1;
     else
     {
          return x * fact(x); 
          --x;
     }
    }
    which is infinitely recursive (and decrementing of x is never executed). The second is functionally equivalent to
    Code:
    int fact(int x)
    {
     if(x == 1)
      return 1;
     else
     {   
          --x;
          return x * fact(x); 
     }
    }
    which, because it decrements x BEFORE calling fact() recursively, is not infinitely recursive.

    Note that your second version will also be infinitely recursive if supplied with a negative argument.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    43
    Got it...Thanks a lot guys !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .NET woes
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 07-18-2006, 02:41 PM
  2. MD2 woes
    By psychopath in forum Game Programming
    Replies: 9
    Last Post: 07-02-2005, 07:46 PM
  3. ASP.NET woes
    By nickname_changed in forum C# Programming
    Replies: 0
    Last Post: 03-20-2004, 04:34 PM
  4. Dll woes
    By Abiotic in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2003, 11:32 AM
  5. cgi woes
    By moi in forum C Programming
    Replies: 1
    Last Post: 09-25-2002, 04:59 PM