User Defined Function

Help Contents

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.

Syntax

A Function uses this syntax:

function ::=
  FUNCTION function-name [ : return-type ]
    { variable-declaration }
    [ function-code ]
  END_FUNCTION

return-type ::= [ Elementary Data Type | Derived Data Type ]

function-code ::= structured-text

Where:

Remarks

Use any of the following for the return-type:

If the return-type is not used, the function does not return a value.

Use any of the following for the program-code:

A function can call other functions.

Examples

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

Standards Compliance

IEC 61131-3 Second Edition: Table 20a.

IEC 61131-3 Third Edition: Table 19.

Further Information

Program Units

To learn about other program units.

Variable Declarations

To learn about variable declarations.

ST Statements

To learn about ST Statements.

Glossary

For the meaning of terms used in Fernhill SCADA.