Return values
We have already said that to return something from a function we need to use the return
statement, followed by what we want to return. There can be as many return
statements as needed in the body of a function.
On the other hand, if within the body of a function we do not return anything, or we invoke a bare return
statement, the function will return None
. This behavior is harmless when it is not needed, but allows for interesting patterns, and confirms Python as a very consistent language.
We say it is harmless because you are never forced to collect the result of a function call. We will show you what we mean with an example:
# return.none.py
def func():
pass
func() # the return of this call won't be collected. It's lost.
a = func() # the return of this one instead is collected into `a`
print(a) # prints: None
Note that the whole body of the function is composed only of the pass
statement. As the official documentation tells us, pass...