Likewise
Likewise

Reputation: 30

Recursion decreasing values when it should increase it

I am hoping someone will explain it in a "like I am 5" fashion.

def WorryAboutMoney(money): 
  
    if (money > 3):
        return print()
    else: 
      
        print( money, end = " ") 
        WorryAboutMoney(money+1)
        print( money, end = " ") 
        return
      
  
money = 1
WorryAboutMoney(money) 

Output is:

1 2 3
3 2 1
  1. I don't understand why is the method not completing on the return in the "if". After it returns there, the loop continues until 3 becomes 1 and each time the return in the "else" is called.
  2. I also don't understand why is the loop reducing the numbers, when I've stated nowhere that they should and they should instead increment by 1.

Upvotes: 1

Views: 103

Answers (3)

Stef
Stef

Reputation: 15515

What's going on can be illustrated by printing the depth of recursion:

def WorryAboutMoney(money, depth=0): 
    if (money > 3):
        print(" " * depth + "[call {}]".format(depth))
        return
    else: 
        print("{}[call {}, before] {}".format(" " * depth, depth, money))
        WorryAboutMoney(money+1, depth+1)
        print("{}[call {},  after] {}".format(" " * depth, depth, money))
        return

WorryAboutMoney(1)

Output:

[call 0, before] 1
 [call 1, before] 2
  [call 2, before] 3
   [call 3]
  [call 2,  after] 3
 [call 1,  after] 2
[call 0,  after] 1

Each call prints "before", then pauses while a recursive call is ongoing, then prints "after". The value of money has not changed between the "before" and the "after" prints.

Upvotes: 0

Yuri Feldman
Yuri Feldman

Reputation: 2584

  1. The return print() is causing execution to return to the caller (which in this case is itself, the function WorryAboutMoney - execution returns to the instruction right after the call in the caller, in this case - print( money, end = " ") in the calling function.

  2. Note that in you have 2 calls to print in each instance of WorryAboutMoney: one before the recursive call (which prints the increasing numbers) and one after. Note that the call after the recursive call is only executed after the recursion fully unwraps. It gets executed for the first time (with money=3) right after return print(), then it returns and the same line is executed in the caller (money=2) etc. - that is why you see the numbers decreasing.

Hope that clarifies it.

Upvotes: 1

GalSuchetzky
GalSuchetzky

Reputation: 805

  1. the recursion stops and starts winding back
  2. the numbers are not reduced, they are being printed while the recursion is rewinded.

I'll explain by steps, that will be easier to understand:

  1. you start with call1 to the function and 1 is printed
  2. the function is called again (call2) (between the prints) and now the money is 2 and 2 is printed.
  3. the function is called again (call3) and 3 is printed.
  4. the function is called again (call4) and now money is 4 and the if is accessed, printing a linebreak and returning.
  5. now you go back from call4 to it's caller which is call3, where the money is 3, and print 3 (because the command after the function call is print).
  6. the return statement is reached from call3 and it returns to call2, to the same place - the print, and 2 is printed.
  7. once again you get to the return statement and now return to call1 which prints 1 and returns
  8. done. output will be:
1 2 3
3 2 1

hope that it was "like you're 5" enough.

Upvotes: 2

Related Questions