We're going to be discussiing functions, parameters and return values and their relative scopes.
In the above diagram there are four scopes, in all. The
compute_bill() function exists in Global Scope. The red container is, Function Scope. The blue and green containers are each, Block Scope.
Notice where the
return statement is? In function scope. A return statement in the other two would be also in that scope since they would have the same effect. Send a return value back to the calling scope, which in this instance is global.
Notice also that we sent
groceries to the function as the parameter. This object exists in global scope and since it is a list, it is not copied so should not be mutated in the function (best practice). But we do iterate over it and peel off data which is accumulated in a local running total.
total is in function scope. And, in this case, we are mutating the global object,
stock which is accessible from inside the function.
The return statement passes this local value (not the variable
total, though) back to the caller scope, where it is then printed.
The example shows that we could have printed before returning, or after with no apparent difference. The real difference though is that now we have the value in global scope.
your_bill = compute_bill(groceries)
Now we have it stored in this scope, as well, so may continue to use this value in further computation or tabulation. Making sense?