0
Why it does not stop?
I am puzzled. Why is it increasing by inner function step-by-step (level-1) until level=1 and returning level remains the same? Return should stop running code, true? https://code.sololearn.com/c058CCl0q135/?ref=app
2 Answers
+ 4
It's calling the function again recursively. The end result is basically the same as 5+4+3+2+1
When the function is called the first time the value 5 is passed into the function and the else statement is ran, which returns 5+func(4), this in turn returns 4 + func(3) which returns 3 + func(2) and then 2 + func(1) which will return 1.
5 + func(4)
|
4 + func(3)
|
3 + func(2)
|
2 + func(1)
|
1
- 2
the output is 15 & correct..
what exactly is your doubt..??
this code is an example of recursion of function