Function Block

A function block is a POU (program organization unit), which returns one or several values when executed.

It is similar to a class in any programming language which can be instantiated and executed with a different value.

Download sample

Function block in the structured text has a similar meaning. Let’s say we have 20 similar pumps in our plant. If we want to model these pumps then we can create 1 function block and then make 20 instances of that function block. So we don’t need to model 20 times. It is similar to a class in C++ where we can make an instance and give the input and get the output.

Object Action:

An action can be used to implement further program code. You can implement this program code in a language that is different from the basic implementation. The basic implementation is the function block or the program under which you have inserted the action. An action does not have its own declarations and uses the data of the basic implementation. This means that the action uses the input/output and local variables of the basic implementation.

Object Method:

Methods are an extension of the IEC 61131-3 standard and a tool for object-oriented programming that is used for data encapsulation. A method contains a declaration and an implementation that includes a series of statements. However, unlike a function, a method is not an independent POU, and it is assigned to a function block or program. You can use interfaces for the organization of methods.

//Declare a method
METHOD TestMethod : REAL
  A           : REAL;
  B           : REAL;  
//Body of the method
TestMethod := A + B;   
//Calling the method
functionMethodResult := myFnBlock.TestMethod(12.0, 23.0);  // functionMethodResult is 35.0

Function Block Example

Function block has input, output  and local variable. Input variables can be passed when calling the function block and the input can be assigned.

//Header of the function block

input1FnBool  : BOOL ;
input2FnReal  : REAL ;
output1FnBool  : BOOL ;
output2FnReal  : REAL ;
value1LocalFnBool : BOOL;
value1RefFnBool : BOOL := TRUE;
value2RefFnReal : REAL := -9.99;

//Body of the function block

IF value1RefFnBool THEN	
output1FnBool := NOT input1FnBool;
output2FnReal :=  input2FnReal * 2.0 ;
output1FnBool :=  input1FnBool;
output2FnReal :=  input2FnReal * 1.0 ;

//Calling the function block Main program

VAR //declare few variable we can assign fn block output to those
input1BoolMain  : BOOL ;
input2RealMain  : REAL ;
output1BoolMain  : BOOL ;
output2RealMain  : REAL ;
value1RefBoolMain : BOOL;
value2RefRealMain : REAL;
myFnBlock : TESTFunctionBlock;

//Body of the MAIN program

// value1RefFnBool  is input
myFnBlock.value1RefFnBool := TRUE;  
//Error 'value1LocalBool' is no input of 'TESTFunctionBlock'
//myFnBlock.value1LocalBool := TRUE; 
myFnBlock.input1FnBool := TRUE;
myFnBlock.input2FnReal := 100.0;
value2RefRealMain := myFnBlock.value2RefFnReal;
output2RealMain:= myFnBlock.output2FnReal;

We have also couple of persistent variables in the function block.

From the main program, we need to call the function block in the following way.


If we don’t call then the output will not be updated. Also, note that we can’t assign a local variable of the function block from the caller.


Download the test software from the link at the beginning of the page.

See Also