Why do beginners confuse print() and return? — Python
print()
and return
are very different things, as all programmers come to learn eventually. And yet, you continuously hear the same question asked over and over by beginner programmers:
What is the difference between
print()
andreturn
?
Responses to this question often give basic definitions of the two, and are often met with incredulity from more experienced programmers that these two things can be mixed up. I’ll give a basic definition of the two, suitable for beginners, but I’ll also attempt to answer the question that I never really see the experienced programmers tackle— why were these being mixed up in the first place?
Beginner Definitions of print()
and return
Here are basic definitions of print()
and return
below. I recognise that these definitions will not satisfy more advanced programmers, but for the time being let’s keep it simple:
print()
is used to display text to a command prompt window.return
is used to return a value from inside a function to code that lives outside a function.
These are two different things, right? So then…
Why the confusion?
The confusion comes because beginner programmers only really think in terms of one function at a time. Here’s an example of what I mean:
Beginners often start out with print()
first. So when they write a function and call it, it will something like this:
def add(num1, num2):
print(num1 + num2)
>>> add(1, 2)
3
Then they learn return
and they incorporate it into their function. But they still need to print out what they returned, so the code looks something like this instead:
def add(num1, num2):
return num1 + num2>>> print(add(1, 2))
3
Running both versions of the code give you the same result — 3.
And herein lies the problem — because the beginner programmer sees the second example and questions why they needed to add return
in the first place. After all — the first version of the code looked simpler by using print()
directly, and it gave the exact same answer — so why use return
? What is it adding here?
Clearing the fog
The only way to really demonstrate the difference is to use more than one function call, like so:
def add(num1, num2):
return num1 + num2>>> num3 = add(1, 2)
>>> num4 = add(num3, 3)
>>> print(num4)
6
Here, we are calling add()
once to add 1 and 2 together. return
returns the value of this, 3, back to a variable. This variable is then fed back into the add()
function again to add 3 to it. The final answer, 6, is what then gets printed out.
And here is where the difference between return
and print()
is truly shown. Because you need to use return
to store the interim value in a variable to be then fed back into add()
. It is impossible to replace return
with print()
for this reason — any attempts to do so lead to an exception (and a mess).
A Deeper Truth
So, this is obviously a simple problem — though I hope any beginner programmers reading this are helped in understanding the difference between the two.
But for advanced programmers, I believe there is a deeper truth to this, relevant to when we try to help out newcomers. When a newcomer asks about a problem, we shouldn’t just take the question at face value and give a correct, but possibly incomplete, answer. Rather, we should think about why this person struggled to understand whatever they were asking about in the first place, and tailor our answers thus.