Glossary · PLC and control programming
Local variable
Also known as: VAR, Internal variable, Temporary variable
German: Lokale Variable
In IEC 61131-3 programming, a local variable is a variable declared inside a program organization unit, for example with VAR or VAR_TEMP, that is visible only within that unit.
- PLC programming
- Standards
In one sentence
A local variable is declared inside one PLC program unit and visible only there, which keeps data encapsulated.
Example
Inside FB_Motor, the local variable tStartDelay holds a timer instance that no other part of the program can access.
How it applies
- Engineering: Local variables encapsulate data. In a function block, static local variables (VAR) keep their value between calls; temporary variables (VAR_TEMP) exist only during one execution and must be initialized before use. Using a temporary variable before assigning it is a classic error.
- Reuse: Blocks that use only local data and their interface parameters can be reused and tested in isolation. Hidden dependencies on global data defeat this.
- Maintenance: Local names can repeat across blocks without conflict, which keeps naming simple.
- Documentation: Local variables are normally internal and not part of the external documentation. Exceptions are values that service staff monitor online for diagnosis; list those in the block's reference documentation.
Local variable vs. global variable
A local variable is visible only in its own unit. A Global variable is visible program-wide. Prefer local variables and pass data through interfaces.