Order of Evaluation

The order of evaluation of operands within an expression is not guaranteed. Therefore, if a variable is used twice anywhere within an expression, and there is the possibility of side effects, then the results may not be the expected ones.

For example, consider the source shown in Figure 189, where A is a variable, and FN is a procedure that modifies A. There are two occurrences of A in the expression portion of the second EVAL operation. If the left-hand side (operand 1) of the addition operation is evaluated first, X is assigned the value 17, (5 + FN(5) = 5 + 12 = 17). If the right-hand side (operand 2) of the addition operation is evaluated first, X is assigned the value 18, (6 + FN(5) = 6 + 12 = 18).

Figure 189. Sample coding of a call with side effects
 *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
 *   A is a variable.  FN is procedure that modifies A.
 /free
     a = 5;
     x = a + fn(a);
 /end-free
 
P fn              B
D fn              PI             5P 0
D   parm     5P 0
 /free
     parm = parm + 1;
     return  2 * parm;
 /end-free
P fn              E


[ Top of Page | Previous Page | Next Page | Contents | Index ]