What is the use of a return statement?
Question:
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
- doesn’t the result get automatically returned by whatever result you have without a return statement inside a function?
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?
- Are there a lot more advantages to using return statements than there are disadvantages?
EDIT:
using the print
statement was an example to better present my question. My question does NOT revolve around the print statement.
Thank you.
Answers:
You should use return
statements if you want to compute a value from a function and give it back to the caller.
For your example, if the goal of the function is just to print a fixed string, there’s no good reason to return the return value of print
.
If you don’t return
anything from a function, Python implicitly return
s a None
. print
falls in this category.
In [804]: a = print('something')
something
In [806]: print(a)
None
Similarly with functions that the user defines
In [807]: def f():
...: print('this is f')
...:
In [808]: fa = f() # Note this is assigning the *return value* of f()
this is f
In [809]: print(fa)
None
What you are doing does not require a return statement, you’re right but consider you want to calculate an average.
def calculateAverage(x, y, z):
avg = ((x + y + z)/3)
return avg
Now that you have declared a function that has the ability to take 3 variables and return the calculated average you can now call it from any function and not have to have bulky code.
a = calculateAverage(7, 5, 9)
print("Average is:" + a)
Which will print to screen “Average is: 7”
The power of functions and return values is that you are able to make your code more readable by means of placing a single call to a sophisticated function in your main logic, which means you now have less lines of code and it is more legible/maintainable in the longrun.
Hopefully this helps.
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?
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
- doesn’t the result get automatically returned by whatever result you have without a return statement inside a function?
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?
- Are there a lot more advantages to using return statements than there are disadvantages?
EDIT:
using the print
statement was an example to better present my question. My question does NOT revolve around the print statement.
Thank you.
You should use return
statements if you want to compute a value from a function and give it back to the caller.
For your example, if the goal of the function is just to print a fixed string, there’s no good reason to return the return value of print
.
If you don’t return
anything from a function, Python implicitly return
s a None
. print
falls in this category.
In [804]: a = print('something')
something
In [806]: print(a)
None
Similarly with functions that the user defines
In [807]: def f():
...: print('this is f')
...:
In [808]: fa = f() # Note this is assigning the *return value* of f()
this is f
In [809]: print(fa)
None
What you are doing does not require a return statement, you’re right but consider you want to calculate an average.
def calculateAverage(x, y, z):
avg = ((x + y + z)/3)
return avg
Now that you have declared a function that has the ability to take 3 variables and return the calculated average you can now call it from any function and not have to have bulky code.
a = calculateAverage(7, 5, 9)
print("Average is:" + a)
Which will print to screen “Average is: 7”
The power of functions and return values is that you are able to make your code more readable by means of placing a single call to a sophisticated function in your main logic, which means you now have less lines of code and it is more legible/maintainable in the longrun.
Hopefully this helps.
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?