Functions and function blocks (FUNCTION, FUNCTION_BLOCK)
How a function differs from a function block, instances and memory, the VAR_INPUT/VAR_OUTPUT/VAR_IN_OUT interface, and rules for reusable blocks.
Program organisation units (POUs)
IEC 61131-3 distinguishes three kinds of program organisation unit:
| POU | Memory between calls | Typical use |
|---|---|---|
| FUNCTION | none | computing from inputs: scaling, limiting, conversion |
| FUNCTION_BLOCK | yes, in the instance | an object with state: motor, valve, controller, timer |
| PROGRAM | yes | top level, called by a task |
A function always returns the same result for the same inputs. A function block keeps its variables between calls — for that an instance is declared. On Siemens a function is an FC, a block an FB, and instance memory lives in an instance DB or as a multi-instance inside another FB.
Interface
| Section | Purpose |
|---|---|
| VAR_INPUT | inputs, copied on call |
| VAR_OUTPUT | outputs, read after the call |
| VAR_IN_OUT | passed by reference: the block reads and modifies the caller's variable |
| VAR | internal instance variables, kept between calls |
| VAR_TEMP | temporary, exist only during the call |
FUNCTION_BLOCK FB_Motor
VAR_INPUT
Start, Stop, Feedback : BOOL;
FeedbackTime : TIME := T#3S;
END_VAR
VAR_OUTPUT
Run, Fault : BOOL;
END_VAR
VAR
tFeedback : TON;
END_VAR
Call: Motor1(Start := bStart, Stop := bStop, Feedback := bFb); — each motor has its own instance Motor1, Motor2, …
Rules for reusable blocks
- One instance, one call per cycle. Calling the same instance elsewhere in the program breaks the timers and edges inside it. The code reviewer reports this.
- The block does not touch global variables or I/O directly — everything goes through the interface. Otherwise it cannot be reused or tested.
- A fault latches until explicitly acknowledged (Reset) and does not clear itself when the cause disappears.
- Safe default state: on a fault or loss of communication the block's output goes to a state that creates no hazard.
- Temporary variables (VAR_TEMP) do not keep their value — never use them for memory between cycles.
Tools
- Function Block Generator — motor and valve blocks with feedback monitoring and acknowledgement.
- PLC Code Reviewer — repeated instance calls, undeclared variables, unused outputs.
Connections
Open in Knowledge Graph →Loading the map…
Sources
- IEC 61131-3:2025 Programmable controllers — Part 3: Programming languages — IEC
- TwinCAT 3 PLC Library Tc2_Standard — Beckhoff Automation
Updated 2026-10-01