Confused as to where to use return statement in Python
<p>Sometimes I get confused as to <em>where</em> to use the return statement. I've done the Python track on codecademy and I'm halfway through an Intro to CS class online and <em>still</em> I get it wrong sometimes. I get what it does, it's just that I don't 'get' its placement properly :/</p>

<p>Here's a short example of the same code. </p>

<p>Correct way:</p>

<pre><code>def product_list(list_of_numbers):
    c = 1
    for e in list_of_numbers:
        c = c * e
    return c
</code></pre>

<p>Wrong way (which I did initially):</p>

<pre><code>def product_list(list_of_numbers):
    c = 1
    for e in list_of_numbers:
        c = c * e
        return c
</code></pre>

<p>This probably sounds silly to most of you, but in bigger and more complicated situations I get it properly confused. If someone can clarify it to me once and for all I'd appreciate that a lot.</p>

<p><code>return</code> in a function means you are leaving the function immediately and returning to the place where you call it.
So you should use <code>return</code> when you are 100% certain that you wanna exit the function immediately.</p>

<p>In your example, I think you don't want to exit the function until you get the final value of c, so you should place the <code>return</code> outside of the loop.</p>

python