Hai
Hai

Reputation: 61

Python Return Command In Recursion Function

While learning Python and browsing the internet I stumble upon a piece of code in w3schools.com. I tried to run it using their built-in site Python IDLE and using my own Python 3.9.0 Shell. What I got is two different outputs.

I want to know which output is the correct output and why is it providing two different outputs.

The Codes and Its Output

Built in site Python IDLE Built in site Python IDLE

Python 3.9.0 Shell Python 3.9.0 Shell

Notice that the number 21 is only printed(outputted) once when running the code using Built-in site Python IDLE, while it is printed(outputted) twice when running the code using Python 3.9.0 Shell.

My Own Debugging

I have tried a simple print statement debugging. Checking the result there is only one different, using Python 3.9.0 Shell the last return line is executed and it outputted the last result while using the Built-in site Python IDLE the last return is either not executed or not outputted, in this case, I believe it is the former, and I believe the correct output is the Python 3.9.0 Shell, but I have no idea why are there two different output.

Print Statement Using Python 3.9.0 Shell Result Part 1 Result Part 2

Print Statement Using Built-in site Python IDLE Result Part 1 Result Part 2

Source Code

def tri_recursion(k):
    if(k>0):
           result = k + tri_recursion(k-1)
           print(result)
    else:
           result = 0
    return result

tri_recursion(6)

Upvotes: 4

Views: 499

Answers (3)

pradeexsu
pradeexsu

Reputation: 1145

both are correct you need to understand that the python shell is printing statement's output. when you write :

>>> tri_recursion(6)

it will execute first all the print inside function then it prints value that the last call returns.

Upvotes: 1

fixmycode
fixmycode

Reputation: 8506

Both are correct.

The built-in python on the site is showing you the output of the execution of your program. This is equivalent to running the following program:

if __name__ == '__main__':
    tri_recursion(6)

add this at the end of your code, save it as test.py and run it like this:

python test.py

the results will be the same

The python shell is showing you the output of the REPL (Read-Eval-Print-Loop), the print statement will print to the screen, but the return value of the function is also printed, because of the REPL, there's no way to avoid this, it is so by design.

You could design your function to not return anything, but it wouldn't be recursive anymore.

Upvotes: 1

Abhinav Mathur
Abhinav Mathur

Reputation: 8101

You have added a return result statement at the end. For any IDE, unless you print that value, it wouldn't be displayed. However, IDLE prints the return value as well. Technically, both outputs are correct, since both interpreters are configured to do different actions. As a small example,

def foo():
    return(1)

Running foo() on IDLE gives >>> 1, whereas it gives nothing on other IDE's, as there is no print statement

Upvotes: 2

Related Questions