recursion of the form var += func(var, n-1)
<p>TL/DR How do you evaluate statements of the form <code>var += func(var, n-1)</code>?</p>

<p>edit: By 'evaluate' I mean, how is the value being called by the right side of this statement determined. Within the function given below, <code>var += func(var, n-1)</code> always results in <code>sum += sum</code>. But why is <code>sum</code> always called? why not <code>sum += (n-1)</code>? What determines which value is called by the right side? With the help of a few responses, I think I figured it out (see below), but more light on the subject will always be appreciated. I've only been learning programming for a few days.</p>

<p>I <a href="https://stackoverflow.com/questions/39814341/recursion-think-python-2-exercise-5-5/39814559#39814559">asked a question regarding a recursive function</a> and received an answer that I was satisfied with, but which raised several more questions. I've been looking through previous questions on the topic but I'm still missing  something. It feels like recursion is a fundamental concept that must be grasped completely to succeed with programming, so I'd like to keep asking questions until I figure it out. The answerer offered another example function for me to contemplate:</p>

<pre><code>def foo(sum, n):
    if (n == 0):
        return sum
    sum += foo(sum, n-1)
    sum /= 2
    return foo(sum, n-1)
</code></pre>

<p>I've never seen += used with a function with multiple arguments and I have no idea how to evaluate this. I added <code>print(sum)</code> after <code>sum += foo(sum, n-1)</code> and <code>sum /= 2</code> to try and see what was happening. Running <code>foo(10, 3)</code> resulted in:</p>

<pre class="lang-none prettyprint-override"><code>20
10.0
20.0
10.0
20.0
10.0
20.0
10.0
20.0
10.0
20.0
10.0
20.0
10.0
</code></pre>

<p>I decided to change the <code>+=</code> to <code>*=</code> to see if that would make what was happening more clear. This is that program:</p>

<pre><code>def foo(sum, n):
    if (n == 0):
        return sum
    sum *= foo(sum, n-1)
    print(sum)
    sum /= 2
    print(sum)
    return foo(sum, n-1)

foo(10, 3)
</code></pre>

<p>It produced the following:</p>

<pre class="lang-none prettyprint-override"><code>100
50.0
500.0
250.0
62500.0
31250.0
312500.0
156250.0
24414062500.0
12207031250.0
1907348632812500.0
953674316406250.0
9.094947017729283e+29
4.5474735088646414e+29
</code></pre>

<p>I can trace a few different interwoven patterns in this output, (i.e. the original sum 10 is called in the 1st, 2nd and 4th instances; the output is in groups of two recursions, two recursions, and three recursions; the output of <code>sum/2</code> feeds the input of the 3rd, 5th and 7th instances), but I can't seem to unravel it without knowing how to evaluate the <code>sum *= foo(sum, n-1)</code> or the original <code>sum += foo(sum, n-1)</code>.</p>

<p>In python, <code>x += y</code> is same as <em><code>x = x + y</code> i.e. add <code>y</code> to the value of <code>x</code> and save to <code>x</code></em>.</p>

<p>In your case, <code>sum += foo(sum, n-1)</code> means <em>add value returned by <code>foo(sum, n-1)</code> with <code>sum</code> and save it as <code>sum</code></em>.</p>

<p>It similarly goes for <code>*=</code>, <code>-=</code>, etc. For example: <code>x *= y</code> means <em>multiply y with x and save it as x</em>. Hope that answered your question.</p>

<p>Read: <a href="https://docs.python.org/2/reference/simple_stmts.html#augmented-assignment-statements" rel="nofollow">Augmented Assignment Statements</a> for more information.</p>

python python-3.x recursion