Home > Software engineering >  python beginner here, recursion code and added the print statement , please explain what print state
python beginner here, recursion code and added the print statement , please explain what print state

Time:12-30

My code:

def recursive_sum(num):
    if num==0:
        return 0
    result = num   recursive_sum(num- 1) 
    print(recursive_sum(num- 1) , end = " ")
    
    return result

recursive_sum(3)

0 0 1 0 0 1 3 

I added the print statement just to play with then this output comes up , it is binary but cant figure out further

CodePudding user response:

That code prints 0 0 1 0 0 1 3 and return 6. Let's see why, by tracing what happens.

Call recursive_sum(3)

  • num=3 is not 0.
  • compute `result = 3 recursive_sum(2)
  • to do so Call recursive_sum(2):
    • num=2 is not 0
    • compute `result = 2 recursive_sum(1)
    • to do so Call recursive_sum(1)
      • num=1 is not 0
      • compute `result = 1 recursive_sum(0)
      • to do so call recursive_sum(0)
        • num=0 is 0
        • return 0
      • so result=1 0 = 0
      • print recursive_sum(0)
      • to do so, call recursive_sum(0)
        • num=0 is 0
        • return 0
      • so print argument is 0. PRINT 0
      • return result, which is 1.
    • So in this call result = 2 recursive_sum(1) = 2 1 = 3
    • print recursive_sum(1)
    • to do so call recursive_sum(1)
      • num=1 is not 0
      • result = 1 recursive_sum(0)
      • to compute that, call recursive_sum(0)
        • num=0 is 0
        • return 0
      • so result = 1 0 = 1
      • print recursive_sum(0)
      • to do so, call recursive_sum(0) [Note, we already know that this returns 0 and print 0. But for this time, I'll redo the trace]
        • num=0 is 0
        • return 0
      • so print argument is 0. PRINT 0
      • return result that is 1.
    • argument of print is 1. PRINT 1
    • return result, that is 3
  • So result = 3 recursive_sum(2) = 3 3 = 6
  • print recursive_sum(2)
  • to do so, call recursive_sum(2) [we already know that calling recursive_sum(2) returns 3 and prints "0 0 1"]
  • PRINT 0 0 1 (If you are not conviced, just copy and paste here all the lines from the previous call recursive_sum(2) to the result= of the same level)
  • print argument (recursive_sum(2)) is 3. So PRINT 3.
  • return result aka 6.

So, long story short: you have a double recursion here. Each call to recursive_sum implies two recursive calls, one to compute result, another to print. And both prints things (unless they return 0). I could have summarized things by saying, recursive_sum(3) call recursive_sum(2) twice. Each recursive_sum(2) calls recursive_sum(1) twice (so four times altogether). Each recursive_sum(1) calls recursive_sum(0) twice (but that prints nothing).

Each of the 4 recursive_call(1) prints 0. Each of the two recursive_call(2) prints 1 (plus what their recursive call prints, that is plus two 0). recursive_call(3) prints 3 (plus phat the two recursive_call(2) prints). So it is not surprising that displays contains four 0, two 1, and one 3.

And since the two recursive calls of a call are done before it prints something of its own, it is not surprising that the print of 1 by recursive_call(2) occurs after the two 0 of its recursive call. And that the two 0 0 1 of the two recursive_call(2) occurs before the 3 of recursive_call(3).

CodePudding user response:

The print statement prints the return values in each iteration. When you called recursive_sum(3):

result = 3 recursive_sum(2) result = 2 recursive_sum(1) result = 1 recursive_sum(0)

In the above three calls, print isn't even reached, now:

 *1. recursive_sum(0) returns 0 to recursive_sum(1) via return result
     *2. recursive_sum(1) -> print(recursive_sum(0)) prints 0
     *3. recursive_sum(1) returns 1 to recursive_sum(2) via return result
         *4. recursive_sum(2) -> print(recursive_sum(1)) further calls recursive_sum(0) which returns 0. Hence, it prints 0 1
         *5. recursive_sum(2) returns 3 to recursive_sum(3)
             *6. recursive_sum(3) -> print(recursive_sum(2)) further calls recursive_sum(1) which further calls recursive_sum(0).
             *7. recursive_sum(0) returns 0 so 0 is printed, recursive_sum(1) returns 0 and 1 so 0 and 1 are printed. Now, recursive_sum(2) returned 3, so 0 0 1 3 are printed.
  • Related