A **User Defined Function** is a sequence of statements which may return an optional value.

A function that returns a value can be called from an Expression.

A function that does not return a value can be called through a Structured Text Function Call.

A **Function** uses the following syntax:

Function name [ : return-type ] variable-declarations statement; statement; ... statement; End_Function

Where:

- name is any suitable Identifier.
- return-type is an optional Elementary Data Type, or Derived Data Type defining the return type of the function. If not specified, the function does not return a value.
- variable-declarations is zero or more Variable Declarations.
- statement is any Structured Text Statement.

A function to calculate the standard deviation of four values:

Function CalcStdDev : LREAL var_input v1,v2,v3,v4 : LREAL; end_var var SumOfSquares : LREAL; Mean : LREAL; end_var (* Calculate the mean *) Mean := (v1 + v2 + v3 + v4) / 4; (* Calculate the standard deviation *) SumOfSquares := (v1 - Mean) ** 2 + (v2 - Mean) ** 2 + (v3 - Mean) ** 2 + (v4 - Mean) ** 2; CalcStdDev := sqrt( SumOfSquares / 4 ); End_Function

Table 20a - IEC 61131-3 Second Edition.

Table 19 - IEC 61131-3 Third Edition.

To learn about other program units.

To learn about variable declarations.

Structured Text (ST) Statements

To learn about Structured Text (ST) Statements.

For the meaning of terms used in **Fernhill SCADA**.