locked
how does the recursion work n what happens at compile time n how does the stacks r called RRS feed

  • Question

  • int fact(int n)

    {

    if(n==0)

     return (1)

    return fact(n*(n-1))

    }

    • Edited by sumitsrbh Saturday, March 5, 2011 8:33 PM
    Monday, February 21, 2011 9:48 AM

Answers

  •  

    You cannot get a better explanation than this

    http://www.animatedrecursion.com/simple/fact.html

     

    A free hint: Avoid the Textese (SMS language) while post something here

    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:01 AM
    Monday, February 21, 2011 9:56 AM
  • int fact(int n)

    {

    if(n==0)

     return (1)

    return(n*(n-1))

    }

    That is not a recursive function. To get it to work correctly, you should be calling fact() inside the function: return fact(n-1)*n;

     

    Edit: (as an aside, you should be using an unsigned int, and a loop instead of recursion)


    Microsoft Test - http://tester.poleyland.com/
    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:01 AM
    Monday, February 21, 2011 11:17 PM
  • The stack never gets *called*; it is a section of memory that is accessed by special purpose registers in the CPU.... How the stack gets used is important. It is used to retain the return address for function and subroutine calls. As part of the microprocessor, its use is included in the instruction set in the form of instructions that use the stack....PUSH, POP, RETURN....and others.

    It is also used for temporary storage of local variables and at times by code for the same purpose.

    The stack is important in recursion because recursion is the process of a function calling itself, which leads to vastly increased stack space utilization. Running out of stack space happens when recursion goes too far, and is another reason recursion is a difficult programming concept to learn.

    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:01 AM
    Monday, February 21, 2011 9:21 PM

All replies

  •  

    You cannot get a better explanation than this

    http://www.animatedrecursion.com/simple/fact.html

     

    A free hint: Avoid the Textese (SMS language) while post something here

    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:01 AM
    Monday, February 21, 2011 9:56 AM
  • The stack never gets *called*; it is a section of memory that is accessed by special purpose registers in the CPU.... How the stack gets used is important. It is used to retain the return address for function and subroutine calls. As part of the microprocessor, its use is included in the instruction set in the form of instructions that use the stack....PUSH, POP, RETURN....and others.

    It is also used for temporary storage of local variables and at times by code for the same purpose.

    The stack is important in recursion because recursion is the process of a function calling itself, which leads to vastly increased stack space utilization. Running out of stack space happens when recursion goes too far, and is another reason recursion is a difficult programming concept to learn.

    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:01 AM
    Monday, February 21, 2011 9:21 PM
  • int fact(int n)

    {

    if(n==0)

     return (1)

    return(n*(n-1))

    }

    That is not a recursive function. To get it to work correctly, you should be calling fact() inside the function: return fact(n-1)*n;

     

    Edit: (as an aside, you should be using an unsigned int, and a loop instead of recursion)


    Microsoft Test - http://tester.poleyland.com/
    • Marked as answer by Jesse Jiang Tuesday, March 1, 2011 3:01 AM
    Monday, February 21, 2011 11:17 PM