Using Implicit Returns in Functions
So you can return numbers, collection types—basically anything. If you don’t provide a return value, Python will return the value
None. If you don’t even have a
return statement, Python will still return
These last two cases are examples of an implicit return value, and we’ll look at that first. Again, if you don’t provide a return value, or if you don’t even have a
None will be used as the return value.
We’re going to create a variable
result to save what we get when we add
x. But that’s it. If I call
add_one() and provide it a number, we’re expecting
6 because within the function,
6 was computed, but we didn’t do anything with it. Again, if I want to see the return value, let’s save it.
This function didn’t return a value. It didn’t have a
return statement at all, and so Python used
None as its return value. Perhaps the author of this function meant to return the result, and we’ll take a look at how that’s done next when we look at explicit
Become a Member to join the conversation.