What is 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.
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 class in C++ where we can make an instance and give the input and get the output.
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.
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.
//Note return value is assigned to function name variable
FUNCTION F_CalcSum1DimArrayOldSchool : LREAL
A : LREAL;
B : LREAL;
F_CalcSum1DimArrayOldSchool := 0;
F_CalcSum1DimArrayOldSchool := A + B;
If you have a function block then if you want to execute the function block then it must be called.
Function Block example
//Soon we edit this…..
org : DINT :=12;
dest : DINT :=13;
another : DINT := 14;
myTimer : TON;
startTimer : BOOL := FALSE;
myTimer(IN:= startTimer, PT:=T#1S);
IF myTimer.Q = TRUE THEN
org:= org +1;