π±Functions that Don't Return Anything
A curious sub-category of functions in Python.
A function, typically, returns a value. Keyword: "typically". Consider a function like so:
def f(x):
print(x)
This is a completely valid function that takes in a value x
and prints it. Notice how this function does not return anything.
Now, for a contradictory fact: all Python functions return a value.
How does this work? When we don't return any value, Python does it for us. Let's try this in our interpreter.
>>> def f(x):
>>> ... print(x)
>>> r_val = f("hi")
hi
>>> r_val
>>>
You'll notice how prompting r_val
on the terminal does not return anything β this is because r_val
is None
.
This, we now know a deep fact about Python β that it implicitly returns None
when no other value is returned.
def f(x):
print(x)
# return None # implicitly!
Last updated
Was this helpful?