0

If we call

factorial(n){  
  if(n==0)
    return 1;

  return (n*factorial(n-1));
}

so in stack it will 5,4,3,2,1 after that it will pop 1 and 2 multiply them and push them to the stack. So who is responsible to all that stuff?

And other than 5,4,3,2,1 is any other elements are there in the stack?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Hi Sourav, welcome to stackoverflow. I may not getting this right, but the `factorial(n)` does not have any pop or push operation, it just returns numbers. Can you explain better or provide the code that does so? If this is an exercise, please provide or [translate](http://translate.google.com/) its content to your question (you can edit it your question). – Armfoot Jun 03 '15 at 15:37
  • He may have not explained this very well, but he is talking about how the stack works, the stack does pop and push – Othman Benchekroun Jun 04 '15 at 11:58

1 Answers1

1

Call stack is used in recursion.

From wikipedia: A call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack".

Here you can find a nice explanation on what the recursion is and how it works.

Answer borrowed from Java Basics - How call works.

  • Evaluate arguments left-to-right. If an argument is a simple variable or a literal value, there is no need to evaluate it. When an expression is used, the expression must be evaluated before the call can be made.
  • Push a new stack frame on the call stack. When a method is called, memory is required to store the following information.
    • Parameter and local variable storage. The storage that is needed for each of the parameters and local variables is reserved in the stack frame.
    • Where to continue execution when the called method returns. You don't have to worry about this; it's automatically saved for you.
    • Other working storage needed by the method may be required. You don't have to do anything about this because it's handled automatically.
  • Initialize the parameters. When the arguments are evaluated, they are assigned to the local parameters in the called method.
  • Execute the method. After the stack frame for this method has been initialized, execution starts with the first statement and continues as normal. Execution may call on other methods, which will push and pop their own stack frames on the call stack.
  • Return from the method. When a return statement is encountered, or the end of a void method is reached, the method returns. For non-void methods, the return value is passed back to the calling method. The stack frame storage for the called method is popped off the call stack. Popping something off the stack is really efficient - a pointer is simply moved to previous stack frame. This means that the current stack frame can be reused by other methods. Execution is continued in the called method immediately after where the call took place.

You can also find some good answers here.

Community
  • 1
  • 1
Tim Rijavec
  • 1,770
  • 22
  • 27