Local Variable Table

This topic discusses the following subjects:

Understanding Local Variables

Declaration Types for Local Variables

Data Type Checking for Local Variables

Does Your Program Need to Use Local Variables?

How to Hide/Show the Local Variable Table

How to Make Assignments in the Local Variable Table

Local Variable Table Example

Understanding Local Variables

Back to Top

Each POU in your program has its own Local Variable Table, with 64 bytes of L memory. These Local Variable Tables allow you to define variables that are restricted in scope: a local variable is only valid inside the POU where it was created. By contrast, global symbols, which are valid in every POU, can only be defined in the Symbol Table/Global Variable Table. In cases where you use the same symbolic name (e.g., INPUT1) for a global symbol and a local variable, the local definition takes precedence inside the POU where the local variable has been defined and the global definition is used in the other POUs.

You assign a declaration type (TEMP, IN, IN_OUT, or OUT) and a data type (see SIMATIC and IEC 1131-3 Data Types), but not a memory address, when you make assignments in a Local Variable Table; the Program Editor automatically assigns memory locations in L memory for all local variables.

Declaration Types for Local Variables

Back to Top

The type of local variable assignment you can make depends on the POU where you are making the assignment. The main program (OB1), interrupt routines, and subroutines can use temporary (TEMP) variables. Temporary variables are only available while the block is being executed and are then free to be overwritten when the block is completed. Subroutines can also use call parameters (IN, IN_OUT, OUT).

Declaration Type Description
IN Input parameter provided by the calling POU.
OUT Output parameter returned to the calling POU.
IN_OUT Parameter whose value is supplied by the calling POU, modified by the subroutine, and then returned to the calling POU.
TEMPORARY Temporary variable that is saved temporarily in the local data stack. Once the POU has been executed completely, the value of the temporary variable is no longer available. Temporary variables do not keep their value between POU executions.

Data Type Checking for Local Variables

Back to Top

When you pass local variables as parameters for a subroutine, the data type that you have assigned in the Local Variable Table of that subroutine must match the data type of the value in the calling POU.

Example:

You call SBR0 from OB1, using a global symbol called INPUT1 as an input parameter of the subroutine.

Inside the Local Variable Table of SBR0, you have defined a local variable called FIRST as an input parameter.

When OB1 calls SBR0, the value of INPUT1 is passed to FIRST.

The data types of INPUT1 and FIRST must match.

If INPUT1 is a REAL and FIRST is a REAL, the data types match. If INPUT1 is a REAL but FIRST is an INT, the data types do not match and the program cannot be compiled until this error is corrected.

Does Your Program Need to Use Local Variables?

Back to Top

There are two reasons to use a local variable:

ˇYou want to create portable subroutines that do not make references to absolute addresses or global symbols.

ˇYou want to use interim variables (local variables declared as TEMP) to perform calculations in order to free up PLC memory.

If these descriptions do not fit your situation, you do not need to use local variables; you can make all of your symbolic values global by defining them in the Symbol Table (SIMATIC) or Global Variable Table (IEC).

Viewing/Hiding a Local Variable Table

Back to Top

If you drag the horizontal splitter bar up to the top of the Program Editor window, the Local Variable Table is no longer visible, but it is still there. Drag the splitter bar down to make the Local Variable Table visible again.

How to Make Assignments in the Local Variable Table

Back to Top

Notes:* It is most efficient to make assignments in the Local Variable Table before using local variables in your program. When you use symbolic names in your program, the Program Editor checks first the Local Variable Table of the appropriate POU, and then the Symbol Table/Global Variable Table. If the symbolic name is undefined in both places, the Program Editor treats it as a global symbol: the Program Editor assigns a green wavy underline and encloses the name in double quotation marks, like this: "UndefinedLocalVar". The Program Editor does not automatically re-read the Local Variable Table and make adjustments if you later make an assignment to that symbolic name. In such a case, in order to use the name as a local variable, you must manually delete the quotation marks in your program code and insert a pound symbol in front of the name, like this: #UndefinedLocalVar.

** The maximum limit of input/ output parameters for each subroutine call is 16. If you attempt to download a program that exceeds this limit, an error is returned.

How to Enter Your First Local Variable Assignment

To make an assignment in a Local Variable Table, follow the procedure below.

1.Ensure that the correct POU is displayed in the Program Editor window by clicking, if necessary, on the tab of the desired POU. (Since every POU has its own Local Variable Table, you need to make sure that you are making assignments to the correct POU.)

2.If the Local Variable Table is hidden, drag down the horizontal splitter bar to display it. (See Viewing/Hiding a Local Variable Table.)

3.Choose a row that has the right declaration type for the kind of variable that you want to define, and type a name for the variable in the Name field. (If you are making an assignment in OB1 or an interrupt routine, the Local Variable Table contains only TEMP variables. If you are making an assignment in a subroutine, the Local Variable Table contains IN, IN_OUT, OUT, and TEMP variables.) You do not need to preface the name with a pound symbol in the Local Variable Table. Pound symbols are only used to precede local variables in the program code.
Notes:
* Local variable names are permitted to contain a maximum of 23 alphanumeric characters and underscores. They are also permitted to contain extended characters (ASCII 128 to ASCII 255). The first character is restricted to alpha and extended characters only. It is illegal to use keywords as symbolic names, or to use names that begin with a number or contain characters that are not alphanumeric or in the extended character set.
** Local Variable Table variable names are downloaded and stored in CPU memory. The use of longer variable names may reduce the memory available to store your program.

4.Click the mouse pointer in the Data Type field and use the list box to select an appropriate data type for the local variable.
Note:
When you assign local variables as parameters for subroutines, you must ensure that the data type that you assign to the local variable does not conflict of the operand being used in the subroutine call. (See Data Type Checking above.)

After you supply a value for the Name and Data Type fields, the Program Editor automatically assigns an L memory address to the local variable.

How to Enter Additional Assignments in a Local Variable Table

For OB1 and interrupt routines, the Local Variable Tables display a set of rows pre-defined as TEMP variables. This is the only declaration type that you can use in OB1 or an interrupt routine. To add more rows to the table, simply click a cell in the last row and use the ENTER key to move through that row and down-- a new row is automatically generated.

For subroutines, the Local Variable Tables display a set of rows that are predefined with declaration types in this sequence: IN, IN_OUT, OUT, and TEMP. You cannot change this sequence. Your local variables must follow the same order in this table that the corresponding operands will use when you make operand assignments to the call instruction for the subroutine. If you want to add additional local variables, you must right-click on an existing row and use the popup menu to insert another local variable of the same type as the row you have clicked. Choose Insert>Row to insert the new row above the selected row, or Insert>Below Row to insert the new row below the selected row.

Local Variable Table Example

Back to Top

See Also:

Subroutine

SIMATIC and IEC 1131-3 Data Types

Keyword List