I need help understanding when should Return be used inside a function and when it should be used inside a loop
n = [3, 5, 7] def double_list(x): for i in range(0, len(x)): x[i] *=2 return x # Don't forget to return your new list! print double_list(n)
In the exemple above - the function cause each n[index] to multiply by 2 and the Return statement is outside the loop.
When I put the return inside the loop - CodeCademy gave me an error (console did not).
def my_function(x): for i in range(0, len(x)): x[i] = x[i] * 2 return x
in the exemple above the return is inside the loop.
Can someone explain to me when will I want to use the Return inside and outside a loop?