Declaration of a function block in ST

Syntax
FUNCTION_BLOCK (* optional_begin *) FINAL|ABSTRACT (* optional_end *) FB_name_2
 
(* optional_begin *) USING Namespace_1;
USING Namespace_2; (* optional_end *)
 
(* optional_begin *) EXTENDS FB_name_1 (* optional_end *)
 
(* optional_begin *) IMPLEMENTS interface_1, interface_2, ... interface_n (* optional_end *)
 
(* optional: declaration of variables/instances *)
 
(* optional: declaration of methods *)
 
(* optional: body of function block *)
 
END_FUNCTION_BLOCK

Meaning

declaration of a →function block, FB_name_2 must be an →IEC-identifier.
The declaration is possible within an ST-object or an ST-interface of a C-/C++-block – the declaration is done either within the global →namespace or within a declared namespace. FUNCTION_BLOCK and END_FUNCTION_BLOCK are →keywords for the declaration of the function block.

logi.CAD 3 supports the following function block variants:

  1. function block with a function block body only (the body consists of a set of operations)
    In this case, the function block has no methods implemented.

  2. function block with a function block body and →methods
    A method is similar to a →function; A method can only be defined in the →scope of a →function block type and has implicit access to →static variables of the →function block instance . A method is declared after the section of the declared →variables/→instances and in front of the body of the function block.

  3. function block with methods only
    In this case, the function block has an empty function block body implemented.

The keywords FINAL and ABSTRACT (features of the →object-oriented programming ) are optional. See "Declaration of a method" for an example with an abstract or a final function block.

  • Use the keyword FINAL to indicate that the current function block is not to be a base function block and from which you do not want to derive function blocks. See keyword EXTENDS for details on base function block and derived function block.

  • Use the keyword ABSTRACT to indicate that the current function block is intended to be a base function block from which you want to derive function blocks. There are the following restrictions/consequences:

    • An abstract function block cannot be instantiated.

    • An abstract function block must contain at least one abstract method.

    • As a consequence, a non-abstract function block derived from an abstract function block must include all abstract methods of the abstract function block.

The USING namespace directive after the name of the function block is optional as well. See " Namespaces in ST: usage " for details on this directive.

The keyword EXTENDS (a feature of the →object-oriented programming) is optional as well. Specify EXTENDS , if the function block is to be derived from a different function block (this is the base function block). Specify the name of the base function block after EXTENDS.
The impacts of the keyword EXTENDS are:

  • The derived function block FB_name_2 inherits all variables and methods from the base function block FB_name_1. Of course, you are able to declare its own variables and methods in the derived function blocks. Subsequently, the derived function block may use the inherited variables and methods as well as its own variables and methods.
    Exceptions from this inheritance are the methods that are declared using the keyword PRIVATE or INTERNAL within the base function block.

  • You must use unique names for all variables and methods within the derived function block and within the respective base function block.

  • The base function block itself may be a derived function block. So it is possible to inherit variables and methods over several levels from one function block to another function block. A recursion is not allowed – according to the IEC-standard .

  • If the base function block is changed, the derived function blocks inherits these changes over the corresponding levels.

  • Other restrictions:

    • It is only possible to derive a function block from one base function block that must be a different function block. A derived function block must not inherit from several base function blocks at the same time – this multi-inheritance is not supported by the IEC-standard.

    • It is only possible to derive a function block from a base function block, if this base function block contains methods. The methods might even be contained across several levels, if the base function block itself is a derived function block etc.

    • The derived function block does not automatically inherit the body of the base function block. If you want to execute the body of the base function as well when the derived function block is called, insert the statement SUPER(); within the body of the derived function block. See the following example 3.
      Restrictions for SUPER();:

      • SUPER(); is only allowed within a derived function block, hence when the keyword EXTENDS has been specified for the function block .

      • SUPER(); is only allowed to be used once in the body of the derived function block. Subsequently, SUPER(); is not allowed within iterations statements.

      • SUPER(); must not be specified in methods to call a method. Instead, use one of the calls of a method.

    • Alternative to SUPER();: Move the body of the base function block into a declared method of the base function block and call this method in the derived function block. See the following example 4.

The keyword IMPLEMENTS (a feature of the →object-oriented programming) is optional as well. Specify IMPLEMENTS , if the function block is to implement one or more →interfaces. Specify the name of the interfaces after IMPLEMENTS (separated by a comma).

Subsequently, observe that the function block must contain all methods of the interfaces. If not, a message will indicate the missing methods. Best practice is to use the quick fix of logi.CAD 3 to add the unimplemented methods within the function block. The concrete methods that are declared within the function block are matched with the abstract methods that are declared within the interfaces. Matching is done by using the name of the method only. The methods with the same name must be identical concerning the following aspects:

  • If a data type is required, the data type must be specified in the abstract method as well as in the concrete method. The data type must be of the same type.
    If no data type is required, a data type must be specified neither in the abstract method nor in the concrete method.

  • All variables that are declared within the abstract method must be identical to the variables that are declared within the concrete method.

Example 1: Function block without body and methods
FUNCTION_BLOCK Control
END_FUNCTION_BLOCK

Example 2: Function block implementing interfaces
INTERFACE ISwitch (* declaration of the 1st interface *)
METHOD SwitchState
VAR_INPUT
STATE : BOOL;
END_VAR
END_METHOD
END_INTERFACE
INTERFACE IValve (* declaration of the 2nd interface *)
METHOD Open
END_METHOD
END_INTERFACE
FUNCTION_BLOCK MySwitch IMPLEMENTS ISwitch, IValve
VAR
switchState : BOOL;
valveOpen : BOOL;
END_VAR
METHOD PUBLIC SwitchState
VAR_INPUT
STATE : BOOL;
END_VAR
switchState := STATE;
END_METHOD
METHOD PUBLIC Open
valveOpen := TRUE;
END_METHOD
END_FUNCTION_BLOCK

Example 3: Derived function block and base function block
FUNCTION_BLOCK LightRoom (* the base function block "LightRoom" *)
VAR
Light : BOOL;
END_VAR
VAR_INPUT
Daytime : BOOL;
END_VAR
 
METHOD PUBLIC M1 : INT
m1:=2;
END_METHOD
 
Light := NOT(Daytime);
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK FB_A
METHOD PUBLIC m1 : INT
m1 := 1;
END_METHOD
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK Light2Room
EXTENDS LightRoom (* the derived function block "Light2Room" *)
VAR
Light2 : BOOL; // 2. Licht
Inst1 : FB_A;
v1, v2, v3 : INT;
END_VAR
 
METHOD PUBLIC OVERRIDE M1 : INT
m1 := 3;
END_METHOD
 
METHOD PUBLIC M2
END_METHOD
 
SUPER(); // The body of the base function block "LightRoom" is called here.
Light2:= NOT(Daytime);
 
v1 := SUPER.M1(); // The method "M1" of the base function block "LightRoom" is called here.
v2 := THIS.M1(); // The method "M1" of the current function block "Light2Room" is called here.
v3 := Inst1.M1(); // The method "M1" of the function block instance 'Inst1' (= function block 'FB_A') is called here.
END_FUNCTION_BLOCK

Example 4: Derived function block and base function block
FUNCTION_BLOCK FB1 (* the base function block "FB1" *)
VAR
Var1 : INT;
END_VAR
 
METHOD PUBLIC InitFB1
Var1 := 5;
END_METHOD
 
InitFB1(); (* call of the method "InitFB1" *)
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK FB2 EXTENDS FB1 (* the derived function block "FB2" *)
VAR
Var2 : INT;
END_VAR
 
METHOD PUBLIC InitFB2
Var2 := 5;
InitFB1(); (* call of the method "InitFB1" of the base function block "FB1" - identical to the call of the method within the base function block itself *)
END_METHOD
 
InitFB2(); (* call of the method "InitFB2" of the derived function block "FB2" *)
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK FB3
VAR
iFB2 : FB2;
END_VAR
 
iFB2(); (* call of the instance for the derived function block type "FB2" *)
END_FUNCTION_BLOCK

You are able to use the following language elements for ST within a function block:

The usage of these language elements makes it possible to use other elements (e.g. declaration of STRING variables within the section VAR ... END_VAR ) as well. Such elements are not listed here.