Object-oriented programming with function blocks is - besides of the possibility of extension via EXTENDS - supported by the possible use of interfaces and inheritance. This requires dynamically resolved method invocations, also called virtual function calls.
Virtual function calls need some more time than normal function calls and are used when:
oa call is performed via a pointer to a function block (pfub^.method)
oa method of an interface variable is called (interface1.method)
oa method calls another method of the same function block
oa call is performed via a reference to a function block
oVAR_IN_OUT of a base function block type can be assigned an instance of a derived function block type
Virtual function calls make possible that the same call in a program source code will invoke different methods during runtime.
For more information and in-depth view, refer to:
oMethod for further information on methods.
oTHIS Pointer for using THIS pointer.
oSUPER Pointer for using SUPER pointer.
According to the IEC 61131-3 standard, methods such as normal functions can have additional outputs. They can be assigned in the method call according to syntax:
<method>(in1:=<value> |, further input assignments, out1 => <output variable 1> | out2 => <output variable 2> | ...further output variables)
This has the effect that the output of the method is written to the locally declared variables as given within the call.
Assume that function blocks fub1 and fub2 EXTEND function block fubbase and IMPLEMENT interface1. Method method1 is contained.
Possible use of the interfaces and method calls:
PROGRAM PLC_PRG
VAR_INPUT
b : BOOL;
END_VAR
VAR
pInst : POINTER TO fubbase;
instBase : fubbase;
inst1 : fub1;
inst2 : fub2;
instRef : REFERENCE to fubbase;
END_VAR
IF b THEN
instRef REF= inst1; (* Reference to fub1 *)
pInst := ADR(instBase);
ELSE
instRef REF= inst2; (* Reference to fub2 *)
pInst := ADR(inst1);
END_IF
pInst^.method1(); (* If b is true, fubbase.method1 is called, else fub1.method1 is called *)
instRef.method1(); (* If b is true, fub1.method1 is called, else fub2.method1 is called *)
Assume that fubbase of the upper example contains 2 methods method1 and method2. fub1 overrides method2 but not method1.
method1 is called as shown in the upper example.
pInst^.method1(); (* If b is true fubbase.method1 is called, else fub1.method1 is called *)
For calling via THIS pointer, refer to THIS Pointer.