How would I do a detailed walk-through of a factorial?
<pre><code>def factorial(x):

    if x == 0:
        return 1
    else:
        return x * factorial(x - 1)

print(factorial(3))
</code></pre>

<p>How would I do a detailed walk-through with information on the stack at every call and show the data at every return?</p>

<p>I know I have to draw 3 boxes to show what happens at every call, and the data at every return; I just don't know how the function actually works, and how to implement it. This does not require any coding, but an explanation?</p>

<p>***** Update ****
So something like this?</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>N = 3
4 * factorial (3): 24


N = 2
5 * factorial (4): 120


// and so on....</code></pre>
</div>
</div>
</p>

<p>You can try to add some <code>print</code> lines to know what's happening inside a function call. Such as the following:</p>

<pre><code>def factorial(x):
    print("factorial({}) is called".format(x))
    if x == 0:
        print("Reached base branch")
        return 1
    else:
        print("Reached recursive branch")
        result = x * factorial(x - 1)
        print("Exitted recursive branch")
        return result

print(factorial(3))
</code></pre>

<p>This should let you see how the call to <code>factorial(3)</code> eventually calls <code>factorial(2)</code>, <code>factorial(1)</code>, and <code>factorial(0)</code> and then goes up the recursive hierarchy when the based case is reached.</p>

python recursion stack factorial