Quite a stupid question but...
def gcd(a, b):
if a < b:
a, b = b, a
if a == 0:
return b
elif b == 0:
return a
return gcd(b, a % b)
So when there's no return
at the end of the function, why would the code return None
? When it's called recursively and a
or b
is eventually equal to 0
, it meets the if
condition right? Then it should return the non-zero value.
If neither a
nor b
is equal to zero, neither one of the first two return statements will be executed.
Without the final return, the flow of control reaches the end of the function without hitting a return statement, so the default value of None
is returned.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.