VAR_IN_OUT variables - VAR_IN_OUT, VAR_IN_OUT CONSTANT

VAR_IN_OUT variables are used as the input and output variables of blocks. The variables are passed on through the block (pass-by-reference variable). You can change the variables within the block.

Hint

You cannot read and write VAR_IN_OUT variables externally directly via <FB instance><input output variable> because the value of the passed variable has changed (passed as pointer, call by reference). This also means that the input value for these variables must not be constants or directly assigned bit values (for example, xBit0 AT %MX0). For passing constants, you should use variable with the extended declaration VAR_IN_OUT CONSTANT. For passing the value of a bit variable, you should use a temporary variable.

Example of a workaround for assigning a bit variable to a VAR_IN_OUT input

Declaration of bit variables (xBit0):

VAR_GLOBAL
    xBit0 AT %MX0.1 : BOOL;
    xTemp : BOOL;
END_VAR

Function block with VAR_IN_OUT input xInOut:

FUNCTION_BLOCK FB_Test
VAR_INPUT
    xIn    : BOOL;
END_VAR

VAR_IN_OUT
    xInOut : BOOL;
END_VAR

IF xIn THEN
    xInOut := TRUE;
END_IF

Program that calls the function block; direct assignment of bit variables to the VAR_IN_OUT input (error) and assignment with help from temporary variables (workaround):

PROGRAM Main
VAR
    xIn : BOOL;
    I1  : FB_Test;
    I2  : FB_Test;
END_VAR
// Error C0201: Type 'BIT' doesn't correspond to the type 'BOOL' of VAR_IN_OUT 'xInOut'
I1(xIn := xIn, xInOut:=xBit0);

// Workaround
//xTemp := xBit0;
//I2(xIn := xIn, xInOut := xTemp);
//xBit0 := xTemp;

VAR_IN_OUT CONSTANT variables

Strings are often used for transferring constants to function blocks by means of VAR_IN_OUT variables. When you use the VAR_IN_OUT variables for this purpose, make sure that the string length of the variables that are assigned to the VAR_IN_OUT variables corresponds to the string length of the VAR_IN_OUT variables. You can avoid this by using VAR_IN_OUT CONSTANT.

This type of VAR_IN_OUT variables is supported by compiler versions >= 3.5.2.0.

Properties of VAR_IN_OUT CONSTANT variables

  • The variables are read-only within the function block.
  • These variables permit strings to be transferred without having to think about the string length.
  • In general, you can use VAR_IN_OUT CONSTANT variables for passing constants to functions or function blocks; however, in the case of basic data types, only if the Replace constants compiler option is not selected in the Compileoptions category of the Project Settings dialog box.

Hint

Please note that an initialization value must not be defined for VAR_IN_OUT CONSTANT variables.

Example

PROGRAM PLC_PRG
VAR
 sVarFits : STRING(16);
 sValFits : STRING(16) := '1234567890123456';
 iVar: DWORD;
END_VAR

POU(sReadWrite := '1234567890123456', scReadOnly := '1234567890123456', iVarReadWrite := iVar);
//POU(sReadWrite := sVarFits, scReadOnly := sVarFits, iVarReadWrite := iVar);
//POU(sReadWrite := sValFits, scReadOnly := sValFits, iVarReadWrite := iVar);
//POU(sReadWrite := sVarFits, scReadOnly := '23', iVarReadWrite := iVar);

FUNCTION POU : BOOL
VAR_IN_OUT
 sReadWrite  : STRING(16);    (* string, can be read or written here in POU *)
 iVarReadWrite : DWORD;  (* dword, can be read or written here in POU *)
END_VAR

VAR_IN_OUT CONSTANT
 scReadOnly : STRING(16);  (* string constant, can be only read here in POU *)
END_VAR

sReadWrite := 'string_from_POU';
iVarInPOU := STRING_TO_DWORD(scReadOnly);