Returning Values from a Function
Learn how to return values from a function.
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 with ...