Variable: RETAIN
Retain variables are declared by the keyword RETAIN is added in programming objects in the scope VAR, VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, VAR_STAT, or VAR_GLOBAL.
Syntax for the declaration
<scope> RETAIN
<identifier>: <data type> ( := <initialization> )? // ( ... )? : Optional
END_VAR
<scope> : VAR | VAR_INPUT | VAR_OUTPUT | VAR_IN_OUT | VAR_STAT | VAR_GLOBALAn assignment of inputs, outputs, or memory addresses with the AT keyword is not permitted.
In a POU
VAR RETAIN
iVarRetain: INT;
END_VARIn a GVL
VAR_GLOBAL RETAIN
g_iVarRetain: INT;
END_VARPossible declaration locations
Locally in a program | Only the variable is located in the retain memory area. NoteWhen using redundancy, the entire program with all of its data is located in the retain memory area. |
Globally in a global variable list | Only the variable is located in the retain memory area. NoteWhen using redundancy, the entire global variable list with all of its data is located in the retain memory area. |
Locally in a function block | The entire instance of the function block with all of its data is located in the retain memory area. Only the declared retain variable is protected. |
Locally in a function | The variable is not located in the retain memory area. This declaration does not have any effect. |
Locally and persistently in a function | The variable is not located in the retain memory area. This declaration does not have any effect. |
Tip
Whenever possible, avoid using RETAIN to mark the variables of a function block.