### 3` `Evaluating Functions Without Side-Effects

A function is like a cloze (a text with _____ gaps). To complete a cloze you fill in the right words. To call a function you fill in parameters of the right type. When the function

hours-to-wages: (hours :Int -> :Int) {

hours 40 <= {

hours 10 *

} {

40 10 * hours 40 - 15 * +

} if

} fun

is called, the parameter hours is replaced by an actual integer number and then the body of the function is evaluated to an integer number. For example, the step by step evaluation of 45 hours-to-wages looks like this:

45 hours-to-wages

Substitute the call with the function body, in which the parameter hours has been replaced with the argument value (45).

=> 45 40 <= {

45 10 *

} {

40 10 * 45 40 - 15 * +

} if

Evaluate the condition (45 40 <=) of the if-operator (to false).

=> false {

45 10 *

} {

40 10 * 45 40 - 15 * +

} if

Because the condition is false, replace the evaluated if-operator with its else-part.

=> 40 10 * 45 40 - 15 * +

Evaluate left multiplication operator.

=> 400 45 40 - 15 * +

Evaluate minus operator.

=> 400 5 15 * +

Evaluate right multiplication operator.

=> 400 75 +

Evaluate plus operator.

=> 475

For this particular function, the evaluation can be understood in analogy to simplifying an algebraic expression. Unfortunately, this is not always the case. Some functions have so-called side effects, e.g., because they print some output or save some data in a file in addition to returning a value. Moreover, their behavior may depend on some external state, like the time of day, that is used by the function. In such cases, the substitution model just shown is not adequate.