Method call
To implement a method call, the actual parameters (arguments) are transferred to the interface variables. Alternatively, the parameter names can be omitted.
Depending on the declared access modifier, a method can be called in the following ways: only within its own namespace (INTERNAL), only within its own programming block and its derivatives (PROTECTED), or only within its own programming block (PRIVATE). With the PUBLIC option the method can be called anywhere.
Within the implementation a method can call itself recursively: either directly using the THIS pointer or using a local variable for the assigned function block.
Method call as virtual function call
Inheritance can result in virtual function calls. Virtual function calls allow the same call in a program source code to call different methods during runtime.
The method call is dynamically bound in the following cases:
- You call a method using a pointer to a function block (for example, pFB^.SampleMethod).
In this situation, the pointer can point to instances of the type of the function block and to instances of all derived function blocks. - You call a method of an interface variable (for example, iSample.SampleMethod).
The interface can reference all instances of function blocks that implement this interface. - A method calls another method of the same function block. In this case, the method can also call the method of an extended function block with the same name.
- A method is called via a reference to a function block. In this situation, the reference can point to instances of the type of the function block and to instances of all derived function blocks.
- You assign VAR_IN_OUT variables of a basic function block type to an instance of a derived FB type.
In this situation, the variable can point to instances of the type of the function block and to instances of all derived function blocks.
Example
- The function blocks FB_Sub1 and FB_Sub2 each extend the function block FB_Base.
- FB_Base implements the interface I_Base, which defines the Method1 method.
- FB_Base and FB_Sub1 provide the method Method1. FB_Sub1 thus overwrites or extends the method of the base class FB_Base.
- FB_Sub2 does not provide the method. The function block continues to use the method of the base class FB_Base.
- In the MAIN program, an instance of the base class FB_Base, an instance of the subclass FB_Sub1 or an instance of the subclass FB_Sub2 is assigned to an interface variable and a reference to the base class. Which instance is assigned depends on the value of the variable nVar.
- The method Method1 is called via the interface variable and the reference variable. This method call is dynamic and can be executed for different instances (fbBase, fbSub1, fbSub2). The underlying method implementations differ between the instances.
- The implementation of FB_Base.Method1 is executed for the instance fbBase.
- The implementation of FB_Sub1.Method1 is executed for instance fbSub1 because FB_Sub1 overwrites or extends the base class method.
- The implementation of FB_Base.Method1 is executed for the instance fbSub2, since the subclass FB_Sub2 neither overwrites nor extends the base class method, but uses it unchanged.
Interface I_Base with method Method1:
INTERFACE I_Base
METHOD Method1
Function block FB_Base with method Method1:
FUNCTION_BLOCK FB_Base IMPLEMENTS I_Base
METHOD Method1
Function block FB_Sub1 with method Method1:
FUNCTION_BLOCK FB_Sub1 EXTENDS FB_Base
METHOD Method1
Function block FB_Sub2 without own method:
FUNCTION_BLOCK FB_Sub2 EXTENDS FB_Base
Program MAIN:
PROGRAM MAIN
VAR
nVar : INT;
fbBase : FB_Base;
fbSub1 : FB_Sub1;
fbSub2 : FB_Sub2;
iBase : I_Base;
refBase : REFERENCE to FB_Base;
END_VAR
(* Choosing the desired instances via value of nVar:
0 => fbBase
1 => fbSub1
2 => fbSub2 *)
IF nVar = 0 THEN
iBase := fbBase;
refBase REF= fbBase;
ELSIF nVar = 1 THEN
iBase := fbSub1;
refBase REF= fbSub1;
ELSIF nVar = 2 THEN
iBase := fbSub2;
refBase REF= fbSub2;
END_IF
// Regarding each of the following two calls via interface and via reference:
// If nVar is 0, FB_Base.Method1 will be called for instance fbBase
// If nVar is 1, FB_Sub1.Method1 will be called for instance fbSub1
// If nVar is 2, FB_Base.Method1 will be called for instance fbSub2
iBase.Method1();
refBase.Method1();
Additional outputs
According to the IEC 61131-3 standard, methods and normal functions can have additional outputs declared. When the method is called, variables are assigned to the additional outputs.
For more information see Object Function.
Calling a method recursively
Notice | |
Recursions are primarily used for processing recursive data types such as linked lists. Recursion should be used advisedly. An unexpectedly deep recursion can lead to stack overflow and thus to machine downtime. |
Within its implementation, a method can call itself, either
- directly via the THIS pointer or
- indirectly using a local function block instance of the basic function block
Usually, a compiler warning is issued when such a recursive call is made. If the method has the pragma {attribute 'estimated-stack-usage' := '<estimated stack size in bytes>'}, the compiler warning is suppressed. An implementation example can be found in chapter Attribute 'estimated-stack-usage'.
See also:
- Object Method
- Object Function
- Extending a function block
- Reference Programming: SUPER
- Reference Programming: THIS