I am trying to understand this program but can't get my head around it. Could anyone suggest where i'm going wrong?
def mult(a, b):
if b == 0:
return 0
rest = mult(a, b - 1)
value = a + rest
return value
print("3 * 2 = ", mult(3, 2))
In the above Python Script,
- The line print("3 * 2 = ", mult(3, 2)) is run
- The Mult function is called
- In the Mult Function b == 2 so the if condition return FALSE
- The Line rest = Mult(a, b -1) is next and calls the Mult function again on new values (3,1)
- In the Mult Function b == 1 so the if condition return FALSE
- The Line rest = Mult(a, b -1) is next and calls the Mult function again on new values (3,0)
- In the Mult Function b == 0 so the if condition return TRUE
- The value 0 is returned by the if condtion
- The program Prints "3 * 2 = 0"?
I followed your comments and put this together to make it easier to follow
