Skip to content

ST Functions

Commercial

Included in the public demo Compare editions

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.

FamilyFunctionsParameters / result
TrigonometrySIN, COS, TAN, ASIN, ACOS, ATANIN: numeric → REAL; angles in radians
Two-argument angleATAN2IN1 (y), IN2 (x): numeric → REAL
MathSQRT, EXP, LN, LOGIN: numeric → REAL; LOG is base 10
Absolute valueABSIN: numeric → same type
PowerEXPTIN1, IN2: numeric → widest input type
ExtremaMIN, MAXIN1, IN2, …: numeric, at least two → widest type
LimitLIMITMN, IN, MX: numeric → min(max(IN, MN), MX)
SelectionSELG: BOOL, IN0, IN1 → IN0 when FALSE, IN1 when TRUE
MultiplexerMUXK: INT, IN0, IN1, … → zero-based selected value
TruncationTRUNCIN: REAL → DINT, toward zero
Conversion<S>_TO_<T>All 25 pairs of BOOL, INT, DINT, REAL, TIME, including identity
BitsSHL, SHR, ROL, RORIN: 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.

Open a PLCProgram with Edit, enter this source, then Deploy and Run:

PROGRAM LimitDemand
VAR_EXTERNAL Raw : REAL; Demand : REAL; END_VAR
Demand := LIMIT(MN := 0.0, IN := ABS(Raw), MX := 1200.0);
END_PROGRAM

Bind 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 : REAL
VAR_INPUT value : REAL; factor : REAL; END_VAR
Scale := value * factor;
RETURN;
END_FUNCTION

Call 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.

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 simulationDES (event-driven)
Function availableAll built-in and your own FUNCTIONsAll built-in and your own FUNCTIONs
When it is evaluatedOn every scan, once per simulation tickOnly when the program scans: on start, when a signal it reads changes, when one of its timers runs out, or after its own change
ResultSame inputs give the same resultSame inputs give the same result
Between two evaluationsThe result is at most one tick oldThe result stays at its last value until the next event
DES checkNot applicableA 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.

  • 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.