Comparison
Function or function block: the two tests that decide
Short answer
Run two tests. If any local value must survive between calls, or the POU declares a timer, counter or other function block inside itself, it must be a FUNCTION_BLOCK; only an instance keeps memory from one call to the next. Everything else is a FUNCTION, called directly with no instance.

On this page
Two tests decide it, and they decide it fast. If any local value has to survive from one call of the POU to the next, the POU must be a FUNCTION_BLOCK. If the POU declares a timer, a counter or any other function block inside itself, it must also be a FUNCTION_BLOCK, because only an instance owns memory that lasts between calls. Everything else is a FUNCTION: no instance to declare, and a fresh copy of its local variables on every call. The split comes from IEC 61131-3, and TwinCAT applies it exactly. Below are the two tests, the side-by-side table, one worked example with the arithmetic written out, and a committed decision for every case you meet while structuring a program.
The two tests that decide #
Ask both questions before you create the POU, in this order.
Test 1 — must any local value live longer than one call? A filtered value, a counter that climbs, a setpoint that adapts, a previous output used for hysteresis: any of these forces the FUNCTION_BLOCK. A function's local memory returns to its initial value before every call, so nothing written there survives to influence the next one.
Test 2 — does the POU declare a function block of its own? A TON, a CTU or any other function block used as a local variable needs an instance, and an instance needs persistent memory to live in. The working rule is blunt: a POU that contains a TON or a counter is a FUNCTION_BLOCK.
If both answers are no, create a FUNCTION. That is the whole decision; the rest of this article shows what each type costs and where the choice bites.
What IEC 61131-3 says #
The split is not a TwinCAT quirk; it is the standard. IEC 61131-3, the third part of the IEC 61131 series for programmable controllers, first published in December 1993, defines three program organization units: program, function block and function. In the standard's terms a function yields one return value computed from the inputs of the current call and owns no instance, so nothing about it outlives the call. A function block keeps the internal variables of each of its instances from one execution to the next; that retained memory is the entire practical difference.
TwinCAT documents the same model: a function block is always called via an instance, and the instance is a copy of the function block that holds the values. Two instances of one type never share a value, which is why two machines must never call one shared instance, while two callers of the same stateless function cannot interfere with each other at all.
How TwinCAT puts it to work #
Calling each type #
A FUNCTION is called by name, in structured text or as a box in ladder; no declaration precedes it. A FUNCTION_BLOCK is declared once per use, for example fbDebounce : FB_Debounce;, and every call then runs on that instance, with each output read on it, such as fbDebounce.rOut. In ladder the instance sits on the rung as a box under its instance name, and a timer's output then connects onward just as in connecting a TON output to a function block input in TwinCAT ladder.
The instance runs only where it is called #
Declaring an instance executes nothing. The body updates only on the rung or in the line that actually calls it, which is the same trap that bites when a ladder function block is called from SFC, covered in calling a ladder function block from SFC in CODESYS so it executes.
State outlives the rung #
Because the instance keeps its internals, its outputs can still be set after the calling condition drops. That retained-state behavior is exactly what keeps a CODESYS TON output on after its rung goes false; treat it as a property of the instance and design for it.
VAR_TEMP: scratch memory inside an FB #
TwinCAT allows VAR_TEMP only in programs and function blocks and reinitializes those variables on each call. Inside a function block that is the documented way to get function-like working memory: values that reset every call, while the ordinary locals keep theirs between calls.
One instance per job #
When identical logic serves several motors or stations, create one FUNCTION_BLOCK and declare one instance per station; each instance carries its own memory and nothing is shared. Declaring a function block inside another one, a multi-instance, gives each outer instance its own inner timer or counter — the same pattern shown for multi-instance Modbus blocks and MB_DB links in an S7-1200.
The comparison at a glance #
| Question | FUNCTION | FUNCTION_BLOCK |
|---|---|---|
| Instance before the call | None; call the name directly | One per use, for example fbDebounce : FB_Debounce; |
| Local values between calls | Back to the initial value every call | Kept by the instance from call to call |
| TON or counter declared inside | No memory for one to live in; the POU belongs in a function block | Normal; each outer instance carries its own inner one |
| Results | One return value | Every declared output, read on the instance |
| Typical jobs | Scaling, clamping, unit conversion, comparisons | Filters, ramps, debouncers, counters, totalizers, per-station state |
| What a wrong choice looks like | A function hiding state never accumulates | Works, at the cost of declaring an instance |
Worked example: one filter, both POUs #
Take a first-order filter with a per-call gain of 0.25 on a 10 ms task (an example period), and an input that steps from 0.0 to 80.0. Each call applies the same line:
rMem := rMem + 0.25 * (rIn - rMem);
As a FUNCTION_BLOCK, the instance remembers rMem between calls:
FUNCTION_BLOCK FB_Leaky
VAR_INPUT
rIn : REAL; // value this call
END_VAR
VAR_OUTPUT
rOut : REAL; // filtered value of this instance
END_VAR
VAR
rMem : REAL; // survives between calls of the instance
END_VAR
rMem := rMem + 0.25 * (rIn - rMem);
rOut := rMem;
With the input held at 80.0, the instance computes call by call:
- Call 1:
0.0 + 0.25 * (80.0 - 0.0) = 20.0 - Call 2:
20.0 + 0.25 * (80.0 - 20.0) = 35.0 - Call 3:
35.0 + 0.25 * (80.0 - 35.0) = 46.25 - Call 4:
46.25 + 0.25 * (80.0 - 46.25) = 54.6875
Four calls leave 0.75 * 0.75 * 0.75 * 0.75 = 0.316 of the step still open, so 1 - 0.316 = 0.684, or 68.4 %, is covered; 80.0 times 0.684 gives about 54.7, matching the call 4 value to rounding.
As a FUNCTION, the identical body recomputes rMem from its initial value 0.0 on every call:
FUNCTION F_Leaky : REAL
VAR_INPUT
rIn : REAL; // value this call
END_VAR
VAR
rMem : REAL; // back to 0.0 before every call
END_VAR
rMem := rMem + 0.25 * (rIn - rMem);
F_Leaky := rMem;
Every call returns 0.0 + 0.25 * (80.0 - 0.0) = 20.0, and the function answers 20.0 forever while the instance walks toward 80.0. The state is the difference, and the POU type is where that state lives.
Prove the double-call behavior with the machine stopped before the POU goes into service; a wrong choice here is a logic fault you want to find offline.
The decision for each case #
- Analog scaling, for example raw counts to engineering units: FUNCTION. The result follows from the input of this call alone.
- Clamp, conversion, arithmetic and comparisons on the current inputs: FUNCTION.
- Debounce, filter, ramp, totalizer: FUNCTION_BLOCK; each remembers a value between calls.
- Anything with a TON, CTU or other function block declared inside: FUNCTION_BLOCK, no exceptions.
- Alarm evaluation with hysteresis: FUNCTION_BLOCK; the band needs the previous output.
- Identical logic on several stations: one FUNCTION_BLOCK, one instance per station.
Carry the rule to the next project in one line: FUNCTION while the POU is stateless, FUNCTION_BLOCK the moment a value must survive or an inner instance appears.
Questions asked
Why does a counter inside a function never count past one call?
A function's local memory begins every call at its initial value, so a variable incremented inside returns to zero before the next call can use it. Move the logic into a function block and let the instance hold the count, or keep the variable outside the POU and pass it in and out through in-out parameters.
What is VAR_TEMP for in TwinCAT?
VAR_TEMP declares scratch variables inside a program or a function block that TwinCAT reinitializes on every call; it is not available in a plain function. Use it when a function block needs working memory for this call only, while its ordinary locals keep their values between calls.
How many results can each POU type hand back?
A function returns a single return value from the call. A function block exposes every declared output on its instance, so one call can deliver several results at once, such as a filtered value together with a fault flag. When a calculation grows a second result, that alone can tip the choice toward a function block.
Does the choice differ between ladder and structured text?
No. Instance and state behavior belong to the POU type, not the language. The same function block declared in structured text and placed on a ladder rung keeps its state in both, and a function is stateless in both. Only the calling syntax changes: a box under an instance name in ladder, an instance call in structured text.