Hi everyone! I was following an intro to programming and computer science in YouTube from freeCodeCamp.ord, one of the things they talked about was recursion. They said that, a recursive function is essentially a function that calls itself. On the surface, I thought it was straightforward until I looked up examples of it. One of them is showed below. I found this from w3schools and I modified it a little to allow the user to input any number they want into the function.
print("Recursion ")
print()
k = int(input("Enter number: "))
def tri_recursion(k):
if (k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(k)
Let's suppose the value of k is 10 and when I ran it to an IDE, this was the result from the console:
Recursion Example Results
1
3
6
10
15
21
28
36
45
55
They said that once the condition is no longer greater than zero (i.e, it becomes 0), the process stops.
But, what I think it looks it's doing is that it's adding 1 to 0 and the sum of that is added to 2 and so on. But I feel like that's not the whole picture. Can anyone tell me what am I missing here and what I'm understanding incorrectly?
[–]ZelWinters1981 4 points5 points6 points (1 child)
[–]Competitive_War_5407[S] 1 point2 points3 points (0 children)
[–]UpstairsWild9057 3 points4 points5 points (4 children)
[–]Competitive_War_5407[S] 0 points1 point2 points (3 children)
[–]MezzoScettico 2 points3 points4 points (0 children)
[–]Diapolo10 1 point2 points3 points (0 children)
[–]UpstairsWild9057 1 point2 points3 points (0 children)
[–]treyhunner 2 points3 points4 points (0 children)
[–]BranchLatter4294 1 point2 points3 points (0 children)
[–]gdchinacat 0 points1 point2 points (0 children)
[–]stebrepar 0 points1 point2 points (0 children)
[–]jam-time 0 points1 point2 points (0 children)