- #1
uestions
- 22
- 0
I have the code (space = _):
def recursion0(x):
____if x == 1:
________return 1
____else:
________recursion0(x-1)
________print x
I know values 2-x will print, but I cannot figure out how to map the call stack. Below are my thoughts.
Let's set x = 3. 3 goes to 2 in recursion0(3-1), and then 2 goes to 1 in recursion0(2-1). Nothing is returned because there are no return statements, except for 1 which is the value given to recursion0(2). Then 2 is printed because x = 2. I still don't know what happens after this so 3 is printed. What happens after this so 3 is printed?
Another question, why does x print after 1 is returned (and not before)?
def recursion0(x):
____if x == 1:
________return 1
____else:
________recursion0(x-1)
________print x
I know values 2-x will print, but I cannot figure out how to map the call stack. Below are my thoughts.
Let's set x = 3. 3 goes to 2 in recursion0(3-1), and then 2 goes to 1 in recursion0(2-1). Nothing is returned because there are no return statements, except for 1 which is the value given to recursion0(2). Then 2 is printed because x = 2. I still don't know what happens after this so 3 is printed. What happens after this so 3 is printed?
Another question, why does x print after 1 is returned (and not before)?
Last edited: