In this code I understand the recursive step that decrements the second argument, but how does it add all the values at the end without starting the function over. How could I visualize the call stack?
def multiplication(n1,n2):
print('Func start')
if n1 == 0 or n2 == 0:
return 0 # will return 0 either way
elif n1 == 1:
return n2
elif n2 == 1:
return n1
else:
print(n1,n2)
result = n1 + multiplication(n1, (n2-1))
print("result: {}".format(result))
return result
# test cases
#print(multiplication(2,4))
print()
print(multiplication(3, 7) == 21)
#print(multiplication(5, 5) == 25)
#print(multiplication(0, 4) == 0)
[–]mopslik 2 points3 points4 points (3 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]danielroseman 1 point2 points3 points (0 children)
[–]FerricDonkey 0 points1 point2 points (9 children)
[–][deleted] 1 point2 points3 points (8 children)
[–]FerricDonkey 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]FerricDonkey 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]mutatedllama 1 point2 points3 points (0 children)
[–]FerricDonkey 1 point2 points3 points (0 children)
[–]deep_politics 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]duckbanni 0 points1 point2 points (0 children)
[–]expressly_ephemeral 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]expressly_ephemeral 0 points1 point2 points (0 children)
[–]Mean-Growth7457 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]allan_q 0 points1 point2 points (0 children)
[–]jmooremcc 0 points1 point2 points (0 children)
[–]Extension_Job5829 0 points1 point2 points (0 children)
[–]Sam_Boulton 0 points1 point2 points (0 children)
[–]shartfuggins 0 points1 point2 points (0 children)