I have been learning Python for some days now. However, I do not understand return. I have read several explanations from my textbooks and online; they don\'t help!
Your textbook expects you to try things in the interactive interpreter, which shows you values as you enter them. Here's an example of how this looks:
$ python
Python 2.7.5+ (default, Sep 17 2013, 17:31:54)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def sqrt(n):
... approx = n/2.0
... better = (approx + n/approx)/2.0
... while better != approx:
... approx = better
... better = (approx + n/approx)/2.0
... return approx
...
>>> sqrt(25)
5.0
>>>
The key thing here is the difference between expressions and statements. def
is a statement, and produces no result. sqrt
, which the def
block defines, is a function; and functions always produce a return value, such that they can be used in expressions, like sqrt(25)
. If your function doesn't contain return
or yield
this value is None
, which the interpreter ignores, but in this case sqrt returned a number which is automatically printed (and stored in a variable called _
). In a script, you might replace the last line with print sqrt(25)
to get the output to a terminal, but the useful thing about return values is that you can do further processing, such as root=sqrt(25)
or print sqrt(25)-5
.
If we were to run the exact same lines as a script, instead of in interactive mode, there is no implicit printing. The line sqrt(25)
is accepted as a statement of an expression, which means it's calculated - but then the value is simply discarded. It doesn't even go into _
(which is the equivalent of the calculator's Ans button). Normally we use this for functions that cause side effects, like quit()
, which causes Python to exit.
By the way, print
is a statement in Python 2 but a function in Python 3. That's why more and more uses of it have parenthesis.
Here is a script which relies on sqrt
(in this case Python's own version) returning a value:
from math import sqrt
area = float(raw_input("Enter a number: "))
shortside = sqrt(area)
print "Two squares with the area", area, "square meters,",
print "placed side to side, form a rectangle", 2*shortside, "meters long",
print "and", shortside, "meters wide"
return
returns a value from a function:
def addseven(n):
return n + 7
a = 9
b = addseven(a)
print(b) # should be 16
It can also be used to exit a function:
def addseventosix(n):
if n != 6:
return
else:
return n + 7
However, even if you don't have a return
statement in a function (or you use it without specifying a value to return), the function still returns something - None
.
def functionthatisuseless(n):
n + 7
print(functionthatisuseless(8)) # should output None
Sometimes you might want to return multiple values from a function. However, you can't have multiple return
statements - control flow leaves the function after the first one, so anything after it won't be executed. In Python, we usually use a tuple, and tuple unpacking:
def addsevenandaddeight(n):
return (n+7, n+8) # the parentheses aren't necessary, they are just for clarity
seven, eight = addsevenandaddeight(0)
print(seven) # should be 7
print(eight) # should be 8
return
statements are what allow you to call functions on results of other functions:
def addseven(n):
return n+7
def timeseight(n):
return n*8
print(addseven(timeseight(9))
# what the intepreter is doing (kind of):
# print(addseven(72)) # 72 is what is returned when timeseight is called on 9
# print(79)
# 79
The return
keyword is to exit a function and return a value. To let a function return a value, use the return
statement.
As Charlie Duffy comments, when you don't use return
anywhere in your function, Python has an implicit return value of None
. In other words, if you don't tell a function it should return something, then None
is what it returns.
So if you change
def gimmieFive(): return 5
to justdef gimmieFive(): 5
, then someone who runsx = gimmieFive()
will havex == None
, instead ofx == 5
. The 5 just gets thrown away when gimmieFive exits.
You either need to add a print
or write the whole thing into the interactive interpreter to see the return value.
return
makes it possible to get some output/result out of a function. To this value later in the code you have to assign it to a variable:
a = sqrt(25)