Overview
Section titled “Overview”Use built-in functions for calculations, limits, selection and explicit type conversion in a PLCProgram or user POU. A function returns a value; its local variables start fresh on every call. Use a function block when you need persistent state.
Parameters and results
Section titled “Parameters and results”| Family | Functions | Parameters / result |
|---|---|---|
| Trigonometry | SIN, COS, TAN, ASIN, ACOS, ATAN | IN: numeric → REAL; angles in radians |
| Two-argument angle | ATAN2 | IN1 (y), IN2 (x): numeric → REAL |
| Math | SQRT, EXP, LN, LOG | IN: numeric → REAL; LOG is base 10 |
| Absolute value | ABS | IN: numeric → same type |
| Power | EXPT | IN1, IN2: numeric → widest input type |
| Extrema | MIN, MAX | IN1, IN2, …: numeric, at least two → widest type |
| Limit | LIMIT | MN, IN, MX: numeric → min(max(IN, MN), MX) |
| Selection | SEL | G: BOOL, IN0, IN1 → IN0 when FALSE, IN1 when TRUE |
| Multiplexer | MUX | K: INT, IN0, IN1, … → zero-based selected value |
| Truncation | TRUNC | IN: REAL → DINT, toward zero |
| Conversion | <S>_TO_<T> | All 25 pairs of BOOL, INT, DINT, REAL, TIME, including identity |
| Bits | SHL, SHR, ROL, ROR | IN: INT/DINT, N: INT/DINT → type of IN, 16/32-bit operation |
Numeric inputs are INT, DINT or REAL; the widest type is REAL > DINT > INT. SEL/MUX values can also all be BOOL or all TIME. MUX requires at least two values, and K must be INT. TIME conversions use milliseconds.
Use either positional arguments or entirely named arguments:
LIMIT(0.0, Raw, 1200.0) and LIMIT(MN := 0.0, IN := Raw, MX := 1200.0).
Conversions require the declared source type exactly: REAL_TO_INT(2.5) returns 3;
REAL_TO_INT(-2.5) returns -3 (halves away from zero). Use TRUNC to truncate.
BOOL targets use nonzero = TRUE, including NaN. There is no integer overflow wrap.
Quick start
Section titled “Quick start”Open a PLCProgram with Edit, enter this source, then Deploy and Run:
PROGRAM LimitDemandVAR_EXTERNAL Raw : REAL; Demand : REAL; END_VAR Demand := LIMIT(MN := 0.0, IN := ABS(Raw), MX := 1200.0);END_PROGRAMBind the external variables to model signals, using matching names or a signal pragma.
To define a reusable function, put this in a PLCLibrary:
FUNCTION Scale : REALVAR_INPUT value : REAL; factor : REAL; END_VAR Scale := value * factor; RETURN;END_FUNCTIONCall Scale(value := Raw, factor := 2.0) in a program expression. All FUNCTION
inputs are required each call. Assign the result to the function name;
RETURN; exits with its current value. Only elementary parameters/results and
local variables/constants are supported; a FUNCTION cannot instantiate FBs or use
VAR_EXTERNAL, VAR_OUTPUT or VAR_IN_OUT.
Continuous simulation and DES
Section titled “Continuous simulation and DES”Built-in functions work in both simulation modes and return the same result for the same inputs. They have no state and no clock, so the DES check never flags a function call itself. What differs is how often the program that calls them scans:
| Continuous simulation | DES (event-driven) | |
|---|---|---|
| Function available | All built-in and your own FUNCTIONs | All built-in and your own FUNCTIONs |
| When it is evaluated | On every scan, once per simulation tick | Only when the program scans: on start, when a signal it reads changes, when one of its timers runs out, or after its own change |
| Result | Same inputs give the same result | Same inputs give the same result |
| Between two evaluations | The result is at most one tick old | The result stays at its last value until the next event |
| DES check | Not applicable | A function call never triggers a finding; your own FUNCTION bodies are checked like program code |
If a result must follow a value that changes between events, compute it from a signal that
changes with it. A function does not wake a program in DES. For elapsed time, use TON,
TOF or TP; see Writing ST for DES.
Use cases
Section titled “Use cases”- Clamp an actuator demand with LIMIT.
- Calculate geometry with SQRT, ATAN2 and trigonometric functions.
- Select operating values with SEL/MUX.
- Convert explicit numeric types before assigning a narrower value.