Return Values
The absolute value function from the last set prints out a message as part of its work. But what if another programmer who wishes to use our function does…
Why print is not enough
The absolute value function from the last set prints out a message as part of its work. But what if another programmer who wishes to use our function does not want that message printed? Or if another programmer wants the output to be used in another calculation?We typically want functions to output or return some answer. The answer can then be printed in a message or used in a different calculation.
The call expression IS the returned value
If a function returns a value, the function call expression represents the returned value. In the line above, the expression absolute(-10) is equal to the returned value of 10.This function notation is perfectly consistent with the math notation used in algebra. If f(x) = 3x, then the expression f(5) is equal to 15 and the expression f(10) is equal to 30. In exactly the same way, absolute(-10) is equal to 10.
The output or returned value can be used in another calculation.
Python already has a built-in absolute value function called abs(), so in real code you would use that rather than writing your own.
A returned value has to go somewhere
A returned value from a function should be stored, printed, or used in another calculation. Be careful to avoid the error explained below.A function with no return
A function that does not return a value actually returns the value None. So if a function only prints its answer, and you try to store the result of calling it, what you store is None.