Functions that Return ValuesΒΆ

Most functions require arguments, values that control how the function does its job.




(ch04_4)

In this example, the arguments to the abs function are 5 and -5.

Some functions take more than one argument.




(ch04_5)

Note

Of course, we have already seen that raising a base to an exponent can be done with the ** operator.

Another built-in function that takes more than one argument is max.




(ch04_6)

Functions that return values are sometimes called fruitful functions.

In many other languages, a chunk that doesn’t return a value is called a procedure or non-fruitful function.

Fruitful functions still allow the user to provide information (arguments). However there is now an additional piece of data that is returned from the function.

../_images/blackboxfun.png ../_images/squarefun.png



(ch04_square)

As you step through the example in codelens notice that the return statement not only causes the function to return a value, but it also returns the flow of control back to the place in the program where the function call was made.

(ch04_clsquare)




(ch04_clsquare_bad)

Next Section - Variables and Parameters are Local