In function blocks, functions, or methods, you can declare arrays of variable length
in the VAR_IN_OUT
declaration section.
The LOWER_BOUND
and UPPER_BOUND
operators are provided for determining the index limits of the actual used array
at runtime.
Only statically declared arrays (not arrays generated by means of the operator __NEW
) may be passed to an array with variable length.
Syntax of the declaration of a one-dimensional array of variable length
<variable name> : ARRAY[*] OF <data type> := <initialization> ;
|
Name of the array Example: |
|
Data type of an element:
|
|
Optional Initial values for the array of arrays |
Syntax of the declaration of a multi-dimensional array of variable length
<variable name> : ARRAY[*, *] OF <data type> := <initialization> ;
|
Name of the array Example: |
|
Declaration for a two-dimensional array of variable length Formally, an asterisk stands for each dimension of variable length. The dimensions are comma-separated. NOTE: Any number of dimensions of variable length are permitted. |
Syntax of the operators for calculating the limit index
LOWER_BOUND( <variable name> , <dimension number> ) UPPER_BOUND( <variable name> , <dimension number> )
Example
The SUM
function adds the integer values of the array elements and returns the calculated
sum as a result. The sum is calculated across all array elements available at runtime.
As the actual number of array elements will only be known at runtime, the local variable
is declared as a one-dimensional array of variable length.
FUNCTION SUM: INT; VAR_IN_OUT aiData : ARRAY[*] OF INT; END_VAR VAR diCounter : DINT; iResult : INT; END_VAR iResult := 0; FOR diCounter := LOWER_BOUND(aiData, 1) TO UPPER_BOUND(aiData, 1) DO // Calculates the length of the current array iResult := iResult + aiData[diCounter]; END_FOR; SUM := iResult;