Function and FB calls
Function and FB calls
Structured Text has two different call mechanisms that are easy to confuse and that produce two of the most common beginner-and-LLM compile errors. Distinguishing them before writing any call is the single most useful habit you can build:
| Aspect | Function (FUN) | Function Block (FB) |
|---|---|---|
| Has internal state? | No — pure function | Yes — instance carries state across cycles |
| Declared with | FUNCTION foo : RETURN_TYPE | FUNCTION_BLOCK foo |
| Used in code by | direct call: foo(IN := x) | first declare instance, then call the instance: foo_inst(IN := x) |
| Returns | a value via the function name | values via output pins of the instance |
| Example | ABS(-3) → 3, SQRT(2.0) → 1.41… | TON, CTU, R_TRIG, RS, SR and any user-written FUNCTION_BLOCK |
| Storage | none — call disappears after the cycle | one struct per declared instance, lives for the program’s life |
Quick decision tree
- Does the operation need to remember something between one scan cycle and the next (an elapsed time, a counter value, a previous edge)? → It is a Function Block. Declare an instance variable first; then call the instance.
- Otherwise it is probably a Function. Call it directly.
- If you don’t know which one a name is, look it up:
library.read_block("name")— the response carrieskind: "FB"orkind: "FUN".
Sub-pages
- FB instances vs. function calls
— the canonical pattern for declaring an FB instance, plus
the most common LLM error
“X is a Function Block — declare instance first” and its
one-line fix via
project.write.add_variable. - Positional vs. named parameters — both forms are legal IEC 61131-3 but the rules for which is allowed where (functions vs FBs, libraries vs project) are subtle and produce “has no declared parameter list” errors when mixed up.
ForgeIEC notes
- The library catalogue (
library.list_blocksMCP tool) marks every entry as either FB or FUN; never guess based on the name alone. A user-writtenFB_MANUAL_TIMEand a built-inTONare both FBs; a user-writtenFC_GetKcValueand a built-inABSare both functions. TheFB_/FC_prefix is project convention only. - The MCP tool
project.write.add_variablewithiecType: "TON"(or any other FB type) creates the instance variable. The tool returnsFORGE_ERR_FB_INSTANCE_AS_LOCALwhen an FB instance is declared at the wrong scope (e.g. as a function-local in aFUNCTIONPOU, which has no persistent storage).