Local Variables in User Program Steps

Local variables can be declared in user programs steps. Variables declared in this way can only be accessed within that step. The variable values are valid only for the execution of that step. When the task jumps to another step, the local variable values are not retained, even if the task jumps to the same step.

Local variables are different from variables in the variable table. For complex programs, local variables may significantly reduce the number of variables needed in the variable table.

Adding Local Variables to a Step

To add local variables to a user program step:

  1. On the toolbar, click the Add Step Declarations button. Or, right-click the step and choose Add Step Declarations.

  2. A Declarations section will appear.

  3. Declare your variable between the VAR and END_VAR keywords, as shown in the example below.

    Variables can be of REAL, DINT, or DWORD data types, and can be also be fixed-length arrays. Variables can optionally be initialized to a value upon declaration. Variables that are not initialized when declared will default to the value 0.

Example:

Usage

Local variables in a user program step can be used in Expression (113) commands, in command parameters, and in link conditions.

Arrays

Bounds

The lower and upper array bounds are given in brackets. For example, bounds of [0..6] declare an array of 7 items. The upper and lower bounds of the array can be any value, including negative values. For most applications, the lower bound will be zero.

Initializing

Array values can be initialized as shown in the example above. The number of initialized values must equal the number of items in the array.

Parentheses can be used as a repetition factor, where the number preceding the parentheses specifies the number of repetitions.
Examples:

[8,2,3(0),10] is the same as [8,2,0,0,0,10]

[10(0)] initializes an array of length 10 to all zeros.

Size Limits

The maximum length of a local array is 32. A single user program step can have up to 128 items declared, including variables and individual array items.

 

See Also

Expressions Overview | Expression (113) | Creating User Programs


Send comments on this topic.

Copyright © 2024 Delta Computer Systems, Inc. dba Delta Motion