Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Function Blocks and Structured Text

Function Blocks Basics

Learn PLC functions vs function blocks, instances and their memory, inputs and outputs, Function Block Diagram (FBD), and calling blocks, with runnable C stand-ins.

Ladder logic is great for on/off conditions, but real programs repeat the same behavior many times: ten identical conveyors, twenty valves, fifty alarms. Copying and pasting rungs creates a maintenance nightmare. Function blocks (FBs) solve this: you write a behavior once and create many instances of it, each with its own memory.

This lesson explains the difference between a function and a function block, what an instance is, how inputs and outputs work, and how Function Block Diagram (FBD) wires blocks together. The real IDEs cannot run here, so each idea has a C stand-in.

Functions vs function blocks

Function (FC / FUNCTION) Function block (FB)
Memory between calls None Yes, each instance has its own
Same inputs, same output? Always Not necessarily (it has state)
Needs an instance? No, just call it Yes, you declare an instance variable
Examples ABS, SQRT, INT_TO_REAL, your own Scale() Timers, counters, edge detection, PID, your own MotorControl

A function behaves like a mathematical formula. A function block behaves like a small machine that remembers what happened last scan.

Function or function block?

Function No memory, pure calculation
Function block Has memory per instance
Instance One copy with its own data

Timers are function blocks

1 / 2

Instances

A function block type is a template. An instance is a concrete copy of it with its own variables. Think of a class and an object. Two instances of the same timer type have separate elapsed times:

TEXT
VAR
    FillTimer  : TON;       (* instance 1 *)
    HeatTimer  : TON;       (* instance 2 *)
END_VAR

FillTimer(IN := Filling, PT := T#10s);
HeatTimer(IN := Heating, PT := T#30s);

Each call passes the inputs and updates the instance's outputs (FillTimer.Q, FillTimer.ET).

Inputs, outputs and internal state

A function block declares:

  • VAR_INPUT: values given by the caller.
  • VAR_OUTPUT: values the caller reads.
  • VAR: private internal state that persists between calls.

Here is a simple function block, the edge detector from the earlier lesson, in Structured Text (read-only):

TEXT
FUNCTION_BLOCK RisingEdge
VAR_INPUT
    CLK : BOOL;
END_VAR
VAR_OUTPUT
    Q : BOOL;
END_VAR
VAR
    Previous : BOOL;       (* the memory *)
END_VAR

Q := CLK AND NOT Previous;
Previous := CLK;
END_FUNCTION_BLOCK

A stand-in in C

C has no function blocks, but a struct plus a function that takes a pointer to it is the same idea: the struct is the instance's memory, the function is the code.

C Stand-in: a function block as struct + function (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Each instance keeps its own prev, so A and B detect their own edges independently.

A function (no memory)

A function simply computes. It is perfect for scaling, limiting and conversions.

C Stand-in: a pure function, Clamp (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Function Block Diagram (FBD)

In FBD, blocks are boxes with inputs on the left and outputs on the right, joined by wires. Signals flow left to right, and the execution order follows the wiring.

flowchart LR
    S["Start_Button"] --> A["AND"]
    G["Guard_Closed"] --> A
    A --> T["TON<br/>PT = 3 s"]
    T --> M["Motor"]

Figure: an FBD network: the motor starts three seconds after Start and the guard are both true.

The same network in Structured Text:

TEXT
Delay(IN := Start_Button AND Guard_Closed, PT := T#3s);
Motor := Delay.Q;

FBD is popular for analog processing (filter, scale, compare, PID) because the data flow is easy to follow.

Reuse in practice

Typical function blocks in an automation library: Motor (start/stop/fault/feedback), Valve (open/close/feedback), AnalogInput (scaling and alarms), Alarm and PID. Write the block once, test it once, and use it for every device. A bug fix then reaches all instances at once.

C Stand-in: one block, many valves (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Only valve 2 (commanded open, no feedback) faults. The next lessons teach the text language used most often to write function blocks: Structured Text.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a function block have that a function does not?

    Function blocks keep internal state in each instance; functions are stateless.
  2. 2. What is an instance?

    An instance is a declared variable of a function block type.
  3. 3. Why use function blocks for ten identical motors?

    One tested block reused many times is easier to maintain.
  4. 4. In FBD, signals flow:

    Outputs of one block are wired to inputs of the next, left to right.

Summary

  • Functions are stateless; function blocks hold state per instance.
  • Declare an instance, call it every scan, read its outputs.
  • FBD wires blocks left to right; ST calls them as code.
  • Reusable blocks make programs smaller and easier to maintain.

Next: Structured Text Basics