I don't understand the difference between these two functions, why does the one that returns the function work but the other doesn't?
The function returns the greatest common denominator between x and y assuming x > y
Non-working function:
def gcd(x, y):
z = x % y
if z == 0:
return y
else:
gcd(y, z)
Working function:
def gcd(x, y):
z = x % y
if z == 0:
return y
else:
return gcd(y, z)
returnbefore the recursive function call.returnmakes the function "give you back" a value. Suppose you asked me to add up a list of numbers. I could simply add them up and keep the sum to myself... or I could tell you the answer. Telling you the answer (giving it back to you) is returning. Keeping it to myself (or throwing it away) is not returning, which is what you're doing in the first function