Returning Values from a Function
Explore how to return values from functions to make your code reusable and clear. Understand how the return statement works in Python and how it allows data to pass from a function back to the point where it was called.
We'll cover the following...
Trapped in the function
Previously, we saw how writing code for calculating time into a function is a much better way. But we do have a problem. The result from the calculation is trapped within the function. By “trapped,” we mean that we calculate the correct number of seconds, but the value obtained can’t get out of the function, so there’s no way for us to use this value outside of it. To solve this problem, we’ll need to return the result back out to the caller. Let’s see how this works.
Using the return function
The idea behind a function is that it not only can be used to package code so we can reuse it over and over, but it can also do something that will produce some sort of value. In our example ...