6 Programming
6-108
NJ-series CPU Unit Software User’s Manual (W501)
Casting Rules in Expressions in Statements
The implicit cast rules for right-hand arithmetic expressions in assignment statements and for
assigning the value of the right-hand side to the left-hand side also apply to expressions in state-
ments.
Example:
CASE (A+B+C) OF
Result1:
to
ResultN:
to
END_CASE;
Order of Execution of Functions
The order of execution of functions is not defined for functions in expressions. The order of execution
of functions depends on the unit version of the CPU Unit, the version of the Sysmac Studio, and the
notation. Precaution is required in cases where the results of an expression may depend on the
order of execution of the functions, such as in the following cases.
• Expressions that contain more than one function that access the same global variable
• Expressions that contain a function and a variable whose value is changed by that function
Expressions That Contain More Than One Function That Access the Same
Global Variable
In the following example, the order of execution of the three functions is not necessarily the same as
the order of execution of the calculations, which is determined by the priority of the operators. There-
fore, it is possible that the functions are executed in the following order: FUN2, FUN3, and then
FUN1.
result := FUN1() + FUN2() * FUN3();
If all three of the functions in the above expression access and write the same global variable, the
value of the result variable may change depending on the order of execution of the functions.
To ensure that the three functions are always executed in the same order, the expression is broken
up. The following notation is used to execute the functions in the following order: FUN2, FUN3, and
then FUN1.
tmp2 := FUN2();
tmp3 := FUN3();
result := FUN1() + tmp2 * tmp3;
Expressions That Contain a Function and a Variable Whose Value Is Changed
by That Function
The following expression contains a function and a variable whose value is changed by that function.
result := varA + FUN4(out => varA);
In the above expression, the first element on the right side, variable varA, is not necessarily evalu-
ated before FUN4 is executed. Therefore, the value of the result variable may change depending on
the order of varA evaluation and FUN4 execution.
To ensure that varA evaluation and FUN4 execution always occur in the same order, the expression
is broken up. The following notation is used to evaluate varA first and then execute FUN4.
tmp := varA;
result := tmp + FUN4(out => varA);