> For the complete documentation index, see [llms.txt](https://catherine-leung.gitbook.io/data-strutures-and-algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://catherine-leung.gitbook.io/data-strutures-and-algorithms/recursion/how-do-recursive-functions-work.md).

# How do recursive functions work?

To understand how recursion works, we need to look at the behaviour of the run time stack as we make the function calls. &#x20;

{% hint style="info" %}
The runtime stack is a structure that keeps track of function calls and local variables as the program runs.  When a program begins, the main() function is placed on the run time stack along with all variables local to main().  Each time a function is called, it gets added to the top of the runtime stack along with variables and parameters local to that function.  Variables below it become inaccessible.  When a function returns, the function along with it's local variables are popped off the stack allowing access to its caller and its callers variables.
{% endhint %}

Suppose we have the following program:

```cpp
unsigned int factorial(unsigned int n){
    unsigned int rc = 1 ;              //base case result
    if(n > 1)                 //if n > 1 we  have the recursive case
        rc= n * factorial(n-1);  //rc is n * (n-1)!
    }
    return rc;
}
int main(void){
    unsigned int x = factorial(4);
    return 0;
}
```

Lets trace what happens to it with respect to the run time stack:

&#x20; Program begins, main() function is placed on stack along with local variable x. <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxfEKEXYdlI-dBkTA6%2Frecursion1.png?alt=media&amp;token=30acfe78-8733-42d9-8f8b-d50da67952ce" alt="" data-size="original">

factorial(4) is called, so we push factorial(4) onto the stack along with local variables n and rc.<img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxfxunwjAikMPgiwhL%2Frecursion2.png?alt=media&amp;token=5f294347-0004-4740-b998-6dde40dd23d4" alt="" data-size="original">&#x20;

n is 4, and thus the if statement in line 3 is true, thus we need to call factorial(3) to complete line 4. <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxgLYmT77g1SSsJSuL%2Frecursion3.png?alt=media&amp;token=2ca6df80-e2c7-4f00-9897-e6860c956e3a" alt="" data-size="original">&#x20;

{% hint style="info" %}
Note that in each function call to factorial on stack there is a value for n and rc.  Each n and rc are separate variables so changing it in one function call on stack will have no effect on values held in other factorial function calls on stack.
{% endhint %}

Now, the n is 3 which makes the if statement true.  Thus, we make a call to factorial(2). <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxhLpXRO6e1UtvRV5o%2Frecursion4.png?alt=media&amp;token=cd26bda8-93fa-49eb-b936-2e843b6d5a57" alt="" data-size="original">&#x20;

Once again, the if statement is true, and thus, we need to call factorial(1). <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxhYnfKBysw0-tqoY_%2Frecursion5.png?alt=media&amp;token=cc34e727-a2a0-4f7c-bf16-2c738bd46374" alt="" data-size="original">&#x20;

&#x20;Once we make this call though, our if statment is false.  Thus, we return rc popping the stack <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxhjn50FyG5Hh7khOW%2Frecursion6.png?alt=media&amp;token=3c431891-24f7-4d60-bcfe-1cfeabc5c9d2" alt="" data-size="original">&#x20;

Once it is returned we can complete our calculation for rc = 2.  This is returned, popping the stack <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxiFjxdROzkxRwXlWp%2Frecursion7.png?alt=media&amp;token=089439db-c937-4fec-99ed-0169f2fcff1f" alt="" data-size="original">&#x20;

Once it is returned we can complete our calculation for rc = 6.  This is returned, popping the stack <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxiMFW_0383mhuZ3IH%2Frecursion8.png?alt=media&amp;token=7592b777-963e-46a2-ba66-7b81a1c6114b" alt="" data-size="original">&#x20;

Once it is returned we can complete our calculation for rc = 24.  This is returned, popping the stack <img src="https://3939692842-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LFrWzEqLSRHjU6HG9dw%2F-LGxd8qDaQ1QRuKAxM8i%2F-LGxiTj639ZIae4mDiLz%2Frecursion9.png?alt=media&amp;token=42ce6ee4-a60e-45a3-b8a2-5d453e124d8d" alt="" data-size="original">&#x20;
