# Recursion Call-Stack

## Recursion Call-Stack

When our program is executing, a special section of the computer's memory-space is allocated just for our program called the program's `Call Stack`. It is important to understand how a program's `Call-Stack` operates, in order to understand how recursive functions behave when they are executing.

![](https://2312795435-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgCFUL-cVHjEcqM%2F-MWL4l34C79EEhPKoyPD%2F-MWPVwxmJ3Lae6CAtFQf%2FScreen%20Shot%202021-03-22%20at%2010.14.59%20AM.png?alt=media\&token=c08eb4db-5125-4214-a37c-ae60596184f8)

## Simplified Call Stack

![](https://2312795435-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgCFUL-cVHjEcqM%2F-M0-KO91ISxRnRVYcAC8%2F-M0-Kd-ALKqWfRNm109k%2Fstack.png?generation=1581627412857394\&alt=media)

Each time a function is called, the code for the function, as well as space for function arguments and return values, is pushed onto the top of the call stack. When the function finishes execution, then that code it is removed or popped-off of the top of the stack. A stack only allows items to be added to the top-end, so it is called a Last-In-First-Out (LIFO) data structure. In contrast, a queue is a First-In-First-Out data structure. In our programs, the draw() function is constantly being pushed onto the stack, then it is popped off the stack when it's completed executing the code.

## Simplified Stack: Loop vs. Recursive Factorial Function

![](https://2312795435-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgCFUL-cVHjEcqM%2F-M0-KO91ISxRnRVYcAC8%2F-M0-Kd-CiiXQrmJpD-cM%2FrecursiveStack.png?generation=1581627412948822\&alt=media)

Similarly, if we use a factorial function that doesn't use recursion, but uses a for-loop, then only 1 instance of the function is repeatedly pushed onto the stack, and it remains on the stack until it has completed executing, we can see this in the diagram above, where 1 instance of the Factorial(n) function is on the top of the call-stack. However, when we execute a recursive function, then we have many distinct instances of our function's code pushed onto the stack, with the code on the top-most instance being executed until it completes.

## Order of Execution

Below is code for the Recursive version of Factorial Program, we have added several println statements so we can better understand how the code actually executes

```java
void setup(){
int finalResult = factorial( 6 );
println("final result " + finalResult);
}

int factorial( int n){
println("start of factorial n= " + n); //here is the recursive function call
if( n == 1){ //base case 1! = 1 //return 1
println( "base case result = 1");
return 1;
}
int result = n * factorial(n - 1);
println("end of factorial, result= " + result + " n " + n);
return result;
}
```

![](https://2312795435-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0-KLgCFUL-cVHjEcqM%2F-M0-KO91ISxRnRVYcAC8%2F-M0-Kd-EN1wI0_hZGNCI%2FScreenshot%202016-01-20%2015.00.39.png?generation=1581627412986375\&alt=media)

From the above image we can see that the println statement within the factorial function that is located before the function calls itself gets printed in the order that the functions are called. However, we can see that once we reach the base case, then the order changes. As each function is completing it's calculation, it prints the end of factorial statement and then returns that value to the calling instance of the function. So when we design recursive functions, the order of statements: before or after the recursive call has a large impact on the ordering of when the code is actually executed.
