I did a simple function in Python to give me the sum of the numbers from n to 0:
def retorna(n, zero, soma):
if n <= zero:
soma += n
return soma
retorna(n+1, zero, soma)
ret = retorna(1, 5, 0)
print(ret)
The result of the function, as is, is 1. But if I put print (soma)
instead of return soma
(and call function without print
), it gives me:
1, 3, 6, 10, 15.
Why does this happen? I wanted to be able to display only the last result, 15, in the case.
Can you help me and give me another example so I can understand?