Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

FB-level VAR_TEMP referenced from a METHOD

A VAR_TEMP declared in a FUNCTION_BLOCK belongs to the FB body’s call frame. A METHOD has its own stack frame, so it cannot share that temporary.

Erroneous code example:

FUNCTION_BLOCK Bug
VAR_TEMP
    scratch : BOOL;
END_VAR
METHOD PUBLIC DoIt
    scratch := TRUE; // ❌ scratch is not visible here
END_METHOD
END_FUNCTION_BLOCK

To fix, either declare the variable as a member of the FUNCTION_BLOCK (so it becomes part of the instance and is visible to the method), or declare a VAR_TEMP local to the method itself:

FUNCTION_BLOCK Bug
VAR
    scratch : BOOL; // ✅ instance member, visible to methods
END_VAR
METHOD PUBLIC DoIt
    scratch := TRUE;
END_METHOD
END_FUNCTION_BLOCK
FUNCTION_BLOCK Bug
METHOD PUBLIC DoIt
    VAR_TEMP
        scratch : BOOL; // ✅ method-local temporary
    END_VAR
    scratch := TRUE;
END_METHOD
END_FUNCTION_BLOCK