Creating reusable functions and function blocks in Structured Text

The Core Idea

Reusable code expresses a shared calculation or behavior once so several parts of a program can use it. Functions provide calculations; function-block instances can keep separate state for the equipment they represent.

What you will learn

  • Write your own FUNCTION for a calculation, with named inputs and a result.

  • Write your own FUNCTION_BLOCK for logic with memory, and give it clear inputs and outputs.

  • Use one instance per machine, so the logic exists once and every machine keeps its own state.

Why it matters

Copying control code creates multiple places to maintain the same rule. Reuse helps keep that rule consistent while keeping each machine's state separate.

The problem

Two transfer pumps run the same logic. Each has Start, Stop, and a motor fault, and each reports a flow estimate from its drive speed: the pumps are rated 120 L/min at 100 %.

SignalTypeMeaning
START_A, STOP_A, FAULT_ABOOLPump A's buttons and motor fault.
START_B, STOP_B, FAULT_BBOOLPump B's.
SPEED_A, SPEED_BREALDrive speeds, in %. Pump A runs at 75 %, pump B at 50 %.
RUN_A, RUN_BBOOLTRUE runs each pump.
FLOW_A, FLOW_BREALFlow estimates, in L/min.

Pump B's code was copied from pump A's and its tag names changed by hand. The simulator scans every 100 ms. After you lock in your prediction, start both pumps, then switch FAULT B on.

See it go wrong

Switch FAULT B off and FAULT A on: both pumps stop.

Fix it

Write the pump logic once and use it twice. Define a FUNCTION_BLOCK for the run memory, a FUNCTION for the flow, and call one instance per pump with that pump's tags. The checks judge behaviour, so they cannot tell how you wrote it; the worked solution shows the block. Editing pauses the program, so press Run to try your version.

Passing the checks records Structured Text progress on reusable functions and function blocks.

What happened and why

Copied code has to be edited in every copy, and every edit is a chance to miss one. The block keeps the logic in one place, and each call names its machine's tags together:

PumpA(Start := START_A, Stop := STOP_A, Fault := FAULT_A);
PumpB(Start := START_B, Stop := STOP_B, Fault := FAULT_B);

A wrong name in a line like that stands out, and a fix to the logic reaches every pump at once.

Choose the kind of code by whether it needs memory:

FUNCTIONFUNCTION_BLOCK
Keeps values between callsNoYes, in each instance
How you use itCall it in an expression: FLOW_A := FlowLpm(...)Declare an instance, call it, then read its outputs: PumpA.Run
Good forCalculations: scaling, unit conversion, limitsLogic with state: run memory, edges, counts, timers, sequences

Give a block the smallest set of inputs and outputs that describes the machine, and use the plant's words for them. Inside, it should read only its own inputs and members, never a program tag, so every instance behaves the same way wherever it is used.

Common mistakes

  • Copying code for each machine: each copy is a separate chance to miss a rename. Write it once as a block.

  • Using a FUNCTION for logic that must remember: a function starts fresh on every call, so a run memory inside it is lost and the pump runs only while Start is held.

  • Sharing one instance between both pumps: both pumps share one run memory, so one pump's Stop stops the other.

  • Passing another machine's tag to an instance: the block cannot catch a wrong argument. Read each call as one line about one machine.

Platform notes

  • Most platforms keep functions and function blocks in a library that several programs share, and some lock a block once it is tested. A change to a shared block reaches every program that uses it, so test it as carefully as the program that first needed it.

  • IEC 61131-3 (the international standard for programmable controller languages) also defines VAR_IN_OUT for passing a variable by reference, and methods on function blocks in its third edition. This simulator supports VAR_INPUT, VAR_OUTPUT, and internal VAR.

Enable JavaScript to run the embedded simulator and complete the interactive exercises.