Function Output
The final part of the function machine is the function providing output. Let’s take a look at function output in greater detail.
Return Statements
Some functions return values that are useful.
In particular, the type conversion functions convert input to the specified data type
and return the result—calling int("3")
returns the integer value 3
.
Returning a Value
To return a value from functions that we create, we can use a return statement. A return statement has the form:
return some_val
where some_val
is any value.
This function has a single parameter, n
, which is expected to be a
positive integer. It returns the sum 1+2+…+n.
|
|
Console Output
6
Notice that sum_to_n
does not print anything; the output comes from the final
line of the program, which prints the value returned by the function call
sum_to_n(3)
.
Now that we have return statements in our coding toolbox, we will very rarely print anything within a function. If we want to see the value returned by a function then we must print it after calling the function.
Return Terminates Function Execution
When a return statement executes, the function terminates, regardless of whether or not there is any code following the return statement. This means that you must be careful to use return
only when the work of the function has been completed.
This print()
statement in this function never executes, since the function returns before it is reached.
|
|
Console Output
I'm done!
We can use the fact that return
stops the execution of a function intentionally, to force a function to stop execution.
This function prints out the integers 1…n using an infinite while
loop, which nonetheless terminates when the return
statement is executed.
|
|
Boolean Functions
A function that returns a boolean value is known as a boolean function. Perhaps the simplest such function is one that tests an integer to determine if it is even.
|
|
Console Output
True
False
It is conventional to name boolean functions by starting with either is
or has
, which creates a nice semantic effect when reading the code.
For example, reading is_even(4)
communicates to the reader that the function should answer the question, “Is 4 even?” This is a convention so widely used by programmers that it extends to nearly every language.
Let’s return to the is_even
function above, to see how we can use the power of return statements to make it even better.
Since return
terminates the function, we can leave out the else
clause and have the same effect. This is because if n
is even, the return statement in the if
block will execute and the function will end. If n
is odd, the if
block will be skipped and the second return statement will execute.
|
|
This updated version works exactly the same as our initial function.
Additionally, notice that the function returns True
when n % 2 == 0
returns True
, and it returns False
when n % 2 == 0
returns False
.
In other words, the return value is exactly the same as the value of n % 2 == 0
. This means that we can simplify the function even further by returning the value of this expression.
|
|
This version of is_even
is better than the first two, not because it is shorter (shorter isn’t always better), but because it is simpler to read. We don’t have to break down the conditional logic to see what is being returned.
Most boolean functions can be written so that they return the value of a boolean expression, rather than explicitly returning True
or False
.
Check Your Understanding
What does the following code output?
|
|
- 10
- 12
- 4
- 16
What does the following function return?
|
|
"BobBob"
- Nothing (no return value)
undefined
- The value of
Bob