Deep dive Β· expands Platform Β· ch.5

Variable Scope

Two questions for every variable: who can see it, and how long does it live. Get scope wrong and you'll fight ghost values for days. Click a variable to see exactly where it's reachable.

πŸ—‚οΈ scopeRetain Β· lifetime
01 Β· Who sees what

Click a variable β€” watch where it reaches

Nesting matters: a global is visible everywhere; a program-local variable only inside that program; an FB-internal variable only inside that block. Inner scopes see outward, never the reverse.

🌍 Global variable table
gMachineMode gEStopOK
πŸ“¦ PROGRAM Line1
partCount belt1 : FB_Conveyor
🧩 FUNCTION_BLOCK FB_Conveyor (inside belt1)
speed running
πŸ‘† Click any variable to highlight every scope that can read it.
02 Β· How long it lives

The interface & the lifetime

VAR_INPUT  target : REAL; END_VAR // in, by value
VAR_OUTPUT done   : BOOL; END_VAR // out
VAR_IN_OUT buffer : ARRAY[..]; END_VAR // by reference
VAR        count  : UINT; END_VAR // internal, kept
VAR RETAIN total : UDINT; END_VAR // survives power-off

↔️ IN_OUT is by reference. The FB works on the caller's actual variable, not a copy β€” perfect for big arrays you don't want to duplicate every scan.

πŸ”‹ Retain survives power-off. Plain internals reset to their initial value on power-up; mark a variable RETAIN and it keeps its last value across a power cycle.

03 Β· Check yourself

One quick question

Can PROGRAM Line1 directly read speed (an internal variable of FB_Conveyor)?