L D[AR2P#0.0]
Lifetime Supporting Member
The following code snippet was recently posted by Amr Hassan with a query about why the two calls sometimes behaved differently.
There is a fundamental difference between these two calls that may not give the results expected, but, they will always give predictable results.
input1 and input2 are boolean parameters
Method 1 passes DBX0.0 (and DBX0.1) as the actual parameters. This means that when the parameter is referenced inside the FC, DBX0.0 is accessed in the global Data block that is open prior to executing the instruction.
Executes essentially as follows:
Method 2 however, cannot pass the fully addressed variable DB1.DBX0.0 as a parameter, so the editor assigns the value of DB1.DBX0.0 to a local temp and passes the local temp as a parameter.
This means that when the parameter is referenced inside the FC, the code uses the value of DB1.DBX0.0 assigned to the local variable before the call is executed. In this case it does not matter which global data block is open when the input parameter is accessed, or if the value of DB1.DBX0.0 is modified in between calling the block and executing the parameter access inside the FC.
Executes essentially as follows (i've chosen L20.0 as an example local temp address). The V area is the temp local data area of the calling block.
Code:
opn db1
Call fc1
input1: dbx0.0
input2: dbx0.1
call fc1
input1: db1.dbx0.0
input2 : db1.dbx0.1
input1 and input2 are boolean parameters
Method 1 passes DBX0.0 (and DBX0.1) as the actual parameters. This means that when the parameter is referenced inside the FC, DBX0.0 is accessed in the global Data block that is open prior to executing the instruction.
Code:
A input1
Executes essentially as follows:
Code:
LAR1 P#DBX0.0
A [AR1,P#0.0]
Method 2 however, cannot pass the fully addressed variable DB1.DBX0.0 as a parameter, so the editor assigns the value of DB1.DBX0.0 to a local temp and passes the local temp as a parameter.
This means that when the parameter is referenced inside the FC, the code uses the value of DB1.DBX0.0 assigned to the local variable before the call is executed. In this case it does not matter which global data block is open when the input parameter is accessed, or if the value of DB1.DBX0.0 is modified in between calling the block and executing the parameter access inside the FC.
Code:
A input1
Executes essentially as follows (i've chosen L20.0 as an example local temp address). The V area is the temp local data area of the calling block.
Code:
//before the call to the FC
OPN DB1
A DBX0.0
= L20.0
//then in the FC
LAR1 P#20.0
A V[AR1,P#0.0]