0
How these recursion works?
hey, can anyone explain to me how these recursion works? def Sum(n): if n == 0 s=0 print (s) else: s=n+Sum(n-1) return s print Sum(10) many thanks :)
4 Respuestas
0
Recursion is when one function call itself. In this example first n = 10 then
s = 10 + 9 + 8 ... while n = 0 and then it print(0).
0
but s=n+Sum(n-1)
means:
I. 10+10-1= 19
II. 9+ 9 -1= 17
III. 8+ 8 -1=15
.
.
.
..
right?
0
No, s=n+Sum(n-1)
means:
I. 10+sum(10-1)(function Sum(9)(with 9 as argument) will again run)
II. 10+ 9 +sum(9-1)=
III. 10+9+8+sum(8-1)
.
.
.
..
0
aah i understand. thanks a lot