Function without return
Function pow
generates a value. Our get_power
should work the same way, thus, we should be able to call it, pass arguments, e.g. 2
and 3
, and use that call as argument to print
. For now we'll put the call directly below the definition1:
def get_power(base, power):
base ** power
print(get_power(3, 2))
None
Unfortunately, it doesn't work like pow
. Instead of 9
we see some weird None
. It's a special value represented by the keyword None
(see full list of keywords in Python, part 1: Statements). So, the call get_power(3,2)
had generated the value None
and print
displayed it.
The default behavior of the interpreter is this: if the function's definition doesn't contain a statement for generating a value, the interpreter automatically generates None
. Our function, lacking that statement, implicitly generates None
thanks to that behavior.
In larger programs definitions and corresponding calls could be placed far away from each other. Often, lots of function definitions are "bundled" in one portion of the program, like so (click to enlarge): Programmers rarely see the definition on the same page as the call. Some advanced code editors even let you quickly peek into the definition when you're writing a call, which helps a lot.