Factorial..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arunraj2002in
    New Member
    • Jun 2007
    • 22

    Factorial..

    Hi All,

    I have a doubt
    [code=c]
    void myfun(int x)
    {
    if(x>0)
    myfun(--x);

    printf("%d",x);
    }

    int main()
    {
    myfun(5)
    return 0;
    }
    [/code]
    Here the output is : 0,0,1,2,3,4. Can you tell why?
    Last edited by sicarie; Jul 25 '07, 03:54 PM. Reason: Code tags are on the right when you reply/start a thread. Please use them.
  • tnga
    New Member
    • Jul 2007
    • 27

    #2
    Originally posted by arunraj2002in
    Hi All,

    I have a doubt
    [code=c]
    void myfun(int x)
    {
    if(x>0)
    myfun(--x);

    printf("%d",x);
    }

    int main()
    {
    myfun(5)
    return 0;
    }
    [/code]
    Here the output is : 0,0,1,2,3,4. Can you tell why?
    That's a recursion, dude...
    it goes recursioning until x=0
    then it prints and come back
    since you have myfun(--x), x is decremented before the function is called... That's why you have two zerous and no five...

    Best regards,

    TNGA

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by arunraj2002in
      void myfun(int x)
      {
      if(x>0)
      myfun(--x);

      printf("%d",x);
      }
      When x is 1, you call myfun(--x). That call prints 0. Then you return to this call and the x is still 0. So you print 0 again.

      Maybe you should:
      [code=cpp]
      void myfun(int x)
      {
      if(x>0)
      myfun(x - 1);

      printf("%d",x);
      }
      [/code]

      Comment

      • arunraj2002in
        New Member
        • Jun 2007
        • 22

        #4
        Originally posted by weaknessforcats
        When x is 1, you call myfun(--x). That call prints 0. Then you return to this call and the x is still 0. So you print 0 again.

        Maybe you should:
        [code=cpp]
        void myfun(int x)
        {
        if(x>0)
        myfun(x - 1);

        printf("%d",x);
        }
        [/code]
        Now if we modify the program:
        void myfun(int x)
        {
        if(x>0)
        myfun(x--);

        printf("%d",x);
        }

        int main()
        {
        myfun(5);
        return 0;
        }

        This leads to Segmentation fault.. why?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by arunraj2002in
          Now if we modify the program:
          void myfun(int x)
          {
          if(x>0)
          myfun(x--);

          printf("%d",x);
          }

          int main()
          {
          myfun(5);
          return 0;
          }

          This leads to Segmentation fault.. why?
          Note that the value of the expression "x--" happens to be the same as "x".
          In other words your resursion never stops and overflows the stack, hence the
          segmentation violation.

          kind regards,

          Jos

          Comment

          Working...