Let me clarify; Let us say that you have 2 functions in Python:
def helloNot():
a = print("Heya!")
helloNot()
Which will print out Heya! without a return statement.
But if we use a return statement in this function:
def hello():
a = print("Heya!")
return a
hello()
This will print out Heya! as well.
I have read and learned that a return statement returns the result back to the function but
In our case let's use the function helloNot()
(our function without the return statement):
our variable a
, which is a print statement returns the result to the function when we call it or am I missing something?
On a side note,
Why and when would we use return statements?
Is it a good habit to start using return statements?
EDIT:
using the print
statement was an example to better present my question. My question does NOT revolve around the print statement.
Thank you.
Normally, when you call a function, you want to get some result. For example, when I write s = sorted([3,2,1])
, that call to sorted
returns [1,2,3]
. If it didn't, there wouldn't be any reason for me to ever call it.
A return
statement is the way a function provides that result. There's no other way to do that, so it's not a matter of style; if your function has a useful result, you need a return
statement.
In some cases, you're only calling a function for its side-effects, and there is no useful result. That's the case with print
.
In Python, a function always has to have a value, even if there's nothing useful, but None
is a general-purpose "no useful value" value, and leaving off a return
statement means you automatically return None
.
So, if your function has nothing useful to return, leave off a return
statement. You could explicitly return None
, but don't do that—use that when you want the reader to know you're specifically returning None
as a useful value (e.g., if your function returns None
on Tuesday, 3
on Friday, and 'Hello'
every other day, it should use return None
on Tuesdays, not nothing). When you're writing a "procedure", a function that's called only for side-effects and has no value, just don't return
.
Now, let's look at your two examples:
def helloNot():
a = print("Heya!")
This prints out Heya!
, and assigns the return value of print
to a local variable, which you never use, then falls off the end of the function and implicitly returns None
.
def hello():
a = print("Heya!")
return a
This prints out Heya!
, and assigns the return value of print
to a local variable, and then returns that local variable.
As it happens, print
always returns None
, so either way, you happen to be returning None
. hello
is probably a little clearer: it tells the reader that we're returning the (possibly useless) return value of print
.
But a better way to write this function is:
def hi():
print("Heya!")
After all, we know that print
never has anything useful to return. Even if you didn't know that, you know that you didn't have a use for whatever it might return. So, why store it, and why return it?