Declaration of internal variables in ST

Syntax
VAR (* optional_begin *) CONSTANT RETAIN NON_RETAIN PUBLIC|PROTECTED|PRIVATE|INTERNAL { instanceParam } { noCodeGeneration } (* optional_end *)
name_1, name_2, ..., name_n : data-type (* optional_begin *) := initial-value { @RELATES_TO := name_A;} (* optional_end *);
name_3, name_4, ..., name_n : STRING[length] (* optional_begin *) := 'initial-value' { @RELATES_TO := name_B;} (* optional_end *);
name_5, name_6, ..., name_n : ARRAY [x..y] OF type (* optional_begin *) := [initial-value_1, initial-value_2, .., initial-value_n] { @RELATES_TO := name_C;} (* optional_end *);
name_7, name_8, ..., name_n : ARRAY [x1..y1, x2..y2, x3..y3] OF data-type; (* Here, initial values and '@RELATES_TO' are possible as well. *)
name_9, name_10, ..., name_n : REF_TO type := REF(name_A);
(* Additional pieces of data, such as partial addresses, are also possible for the variables. *)
END_VAR

Meaning

declaration of one or more internal →variables (also known as "local variables"), name_1 etc. must be →IEC-identifiers.
The declaration of more sections of this kind is allowed. The declaration is possible within the declaration of a →program, of a →function block, of a →function or of a →method.

Providing the optional keyword CONSTANT prevents all variables of this section from being overwritten during program execution (makes them →constants).
Providing the optional keyword RETAIN or NON_RETAIN makes all elements of this section →retentive or non-retentive. RETAIN or NON_RETAIN is possible within the declaration of a program and the declaration of a function block . If variables are declared based on a structured data type within the section with RETAIN or NON_RETAIN, its structure elements (incl. nestings) are treated as retentive or non-retentive alike.

Use the optional keywords for visibility to define the visibility of all elements of this section. These keywords are possible within the declaration of a function block only.

Keyword

Meaning

PUBLIC

Public usage: The variable may be used anywhere where the function block can be used as well.

PROTECTED (or none)

Protected usage (default): The variable may only be used inside the defining function block and its derived function blocks.

If there are no derived function blocks , PROTECTED has the same meaning as PRIVATE .

PRIVATE

Private usage: The variable may only be used from inside the defining function block.

INTERNAL

Internal usage: The variable may only be used from inside the same →namespace.

Deviations from IEC-standard

If you prefer that logi.CAD 3 applies different visibility to the elements without any keyword, define the logi.CAD 3 configuration variable lc3.var.access.default and the value PUBLIC , PRIVATE or INTERNAL . Subsequently, this value will be used as the new default. In the case of the value PROTECTED for the c onfiguration variable, the default PROTECTED is valid as this is specified by the →IEC-standard.
For PUBLIC as default, you are also able to use the logi.CAD 3 configuration variable lc3.var.access.default.public and value TRUE as an alternative. The configuration variable lc3.var.access.default.public takes precedence over lc3.var.access.default.
See the documentation "Administrator's Manual" for details about the configuration variables.
Consequence: →Local variables (= VAR ) of →function blocks without a keyword for the visibility can be used inside the defining function block and its derived function blocks according to the standard. But it is possible to change the configuration so that these variables can be used anywhere where the function block can be used as well.

The optional →pragmas { instanceParam } , { noCodeGeneration } and { @RELATES_TO := ...;} are intended for system integrators who want to store data within the instance structure . The variables within the section become so-called instance parameters due to these pragmas.

A →data type is possible as type for a variable.

See "Supported data types" to learn which data types are supported for the declaration of variables. Use the optional →initial value [:= initial-value] to assign a value to the variable (see "Initialization of variables in ST" for details).
Moreover, it is possible to declare:

Local variables declared within a function block are →static variables . Local variables declared within a function or a method are →temporary variables.

Good to know

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg Moreover, logi.CAD 3 allows you to define a not yet fully specified location for the declaration. See "Declaration of a language element with partial address in ST" for details.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg Moreover, logi.CAD 3 allows you to specify additional data for the declaration . See " Defining description, comment, JSON string or type for variables or data types " for details.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg Within a section VAR...END_VAR, it is also possible to declare function block instances and to declare variables based on an interface.

Example 1: Different sections
VAR RETAIN
var1: BOOL;
var2, var3 : INT;
END_VAR
 
VAR
var4 : STRING[10];
var5: ARRAY [1..2] of BOOL;
var6 : INT;
myRef1: REF_TO INT;
var1 : INT;
END_VAR

Example 2: Function block with differently visible variables
FUNCTION_BLOCK ExampleVisibleVars
VAR PRIVATE // The variables "Var1" and "Var2" may only be used from inside this function block "ExampleVisibleVars".
Var1, Var2 : INT;
END_VAR
VAR // In case of default configuration: The variables "Var3" and "Var4" may only be used from inside this function block as well.
Var3, Var4 : BOOL;
END_VAR
VAR PUBLIC // The variable "Var5" may be used from any location where the function block can be used as well.
Var5 : TIME;
END_VAR
END_FUNCTION_BLOCK