Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Recursion question

Name: Anonymous 2022-09-22 9:36

Sup /prog/. I'm at my wits end. I cannot figure out how this recursive function in python works for the life of me. Will anyone help me out?

def calc_sum(n):
if n==1:
return 1
else:
return n+calc_sum(n-1)

print(calc_sum(3)) # prints 6??

Name: Anonymous 2022-09-23 6:28

>>5
How does the value persist between different function calls? The only way to make it work is a global variable, and I don't use that here.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List