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:

Example

 

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

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: