User Defined Function

Introduction

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:

Example

A function to calculate the standard deviation of four values:

Function CalcStdDev : LREAL
	var_input v1,v2,v3,v4 : LREAL; end_var
	
	var 
		SumOfSq : LREAL;
		Mean : LREAL;
	end_var

	(* Calculate the mean *)
	Mean := (v1 + v2 + v3 + v4) / 4;

	(* Calculate the std deviation *)
	SumOfSq := (v1 - Mean) ** 2 +
				(v2 - Mean) ** 2 +
				(v3 - Mean) ** 2 +
				(v4 - Mean) ** 2;

	CalcStdDev := sqrt( SumOfSq / 4 );
End_Function

Standards Compliance

Table 20a - IEC 61131-3 2nd Edition.

Table 19 - IEC 61131-3 3rd Edition.

Further Information

Program Units

To learn about other program units.

Variable Declarations

To learn about variable declarations.

Structured Text (ST) Statements

To learn about Structured Text (ST) Statements.

Glossary

For definitions of the terms used in Fernhill SCADA.