Declaration of a method

Syntax
METHOD (* optional_begin *) PUBLIC|PROTECTED|PRIVATE|INTERNAL FINAL|ABSTRACT OVERRIDE (* optional_end *) name_1 (* optional_begin *) : type (* optional_end *) 
 
(* optional: declaration of variables/instances *)
 
(* optional: body of method *)
 
END_METHOD
Meaning

declaration of one or more →methods (a feature of the →object-oriented programming), name_1 must be an →IEC-identifier
The declaration of more sections of this kind is allowed. The declaration is possible within the declaration of →function block or a →class. A restricted declaration of a method is possible as →method prototype in an →interface (see "Declaration of an interface incl. method prototypes" for these restrictions).

Observe:

Possible keywords for a method

Use an optional keyword (= modifier) in order to define where the method is visible (when a function block is mentioned in this table, the description also applies to a class as well):

Modifier

Meaning

PUBLIC

Public call: Method may be called from anywhere.

PROTECTED(or none)

Protected call (default): Method may only be called from inside the defining function block and its derived function blocks.

If there are no derived function blocks, PROTECTED has the same meaning as PRIVATE.

PRIVATE

Private call: Method may only be called from inside the defining function block.

INTERNAL

Internal call: Method may only be called from inside the same →namespace.

Moreover, you are able to specify the following keywords:

FINAL 

This method shall not be overridden.

Use FINAL so that the current method cannot be overridden in the derived function blocks (see the following example 5). FINAL cannot be used in conjunction with ABSTRACT.

ABSTRACT 

This method is abstract.

Use ABSTRACT, if the current method should be implemented within a derived function block. The function block in which the abstract method has been declared must be an abstract function block (= a function block with keyword ABSTRACT; see the following example 4). More notes on using ABSTRACT:

  • ABSTRACT must only be used for methods in an abstract function block.

  • ABSTRACT must not be used in combination with OVERRIDE.

  • An abstract method must not contain statements (a body).

As a consequence, all abstract methods of the abstract function block must be implemented in a non-abstract function block derived from the abstract function block.

OVERRIDE 

This method overrides (= overwrites) a different method with the same name.

Use OVERRIDE in order to replace a method of the base function block with the current method within the derived function block (see information on the keyword EXTENDS under "Declaration of a function block in ST" and the following example 3).More notes on using OVERRIDE:

  • You are only able to overwrite methods that are visible in the derived function block. This means that the method must be declared with the keyword PUBLICPROTECTED or INTERNAL within the same namespace.

  • The →signatures of the current method (within the derived function block) and the overwritten method (within the base function block) must match. This means that the name of the methods and the order of all its parameters (i.e. inputs, outputs, in-out variables, and result type) must match.

  • The keywords of the current method and the overwritten method must match as well. Exception: It is allowed to specify the keyword FINAL for the current method.

  • ABSTRACT must not be used in combination with OVERRIDE.

 

The data type : data-type is optional but is required if a return value is assigned within the method (see the method UP in the following example: the assignment UP:= CV;) or the method is used as operand.
See "Supported data types (in ST)" to learn which data types are supported for the declaration of variables. 

Deviations from IEC-standard

For Neuron Power Engineer, the name of a method must be unique within the same →scope. This is in contrast to the →IEC-standard where the →signature is used to unambiguously identify a method.

 

 

Good to know

(grey lightbulb)A method behaves as a →function does.

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

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.

Example 1: Function block with 2 methods for counting up

FUNCTION_BLOCK CounterWithMethod
  VAR
    CV: UINT;                          // Current value of counter
    Max: UINT:= 1000;
  END_VAR
 
  METHOD PUBLIC UP: UINT               // Method for count up by inc
    VAR_INPUT INC: UINT; END_VAR       // Increment  
    VAR_OUTPUT QU: BOOL; END_VAR       // Upper limit detection
 
    IF CV <= Max - INC                 // Count up of current value
      THEN CV:= CV + INC;
        QU:= FALSE;                    // Upper limit reached
      ELSE QU:= TRUE;
    END_IF;
  UP:= CV;                             // Result of method
  END_METHOD
 
  METHOD PUBLIC UP5: UINT              // Count up by 5
  VAR_OUTPUT QU: BOOL; END_VAR         // Upper limit reached
    UP5:= THIS.UP(INC:= 5, QU => QU);  // Internal method call: The method "UP" of the same function block is called.
  END_METHOD
END_FUNCTION_BLOCK
Example 2: A simple example with methods
PROGRAM ExampleCircle
  VAR
    C1 : CIRCLE; // declaration of a CIRCLE instance
    A : REAL; // circle area
    D : REAL; // circle diameter
    C : REAL; // circle circumference
  END_VAR
    
  // initialize the circle instance with a radius of 7.28
  C1(R := 7.28);
  A := C1.AREA(); // get the area
  D := C1.DIAMETER(); // get the diameter
  C := C1.CIRCUMFERENCE(); // get the circumference
END_PROGRAM
 
FUNCTION_BLOCK CIRCLE
  VAR_INPUT
    R : REAL;
  END_VAR
  VAR CONSTANT
    PI : REAL := 3.1415927;
  END_VAR
    
  // calculate the circle area
  METHOD PUBLIC AREA : REAL
    AREA := PI * R * R;
  END_METHOD
    
  // calculate the diameter
  METHOD PUBLIC DIAMETER : REAL
    DIAMETER := 2 * R;
  END_METHOD
    
  // calculate the circumference
  METHOD PUBLIC CIRCUMFERENCE : REAL
    CIRCUMFERENCE := DIAMETER() * PI;
  END_METHOD  
END_FUNCTION_BLOCK
Examples 3: Methods are overwritten.
FUNCTION_BLOCK LIGHTROOM (* the base function block "LIGHTROOM" *)
  VAR
    LIGHT: BOOL;
  END_VAR
 
  METHOD PUBLIC DAYTIME
    LIGHT := FALSE;
  END_METHOD
 
  METHOD PUBLIC NIGHTTIME
    LIGHT := TRUE;
  END_METHOD
  NIGHTTIME();
  END_FUNCTION_BLOCK
 
FUNCTION_BLOCK LIGHT2ROOM EXTENDS LIGHTROOM (* the derived function block "LIGHT2ROOM" *)
  VAR
    LIGHT2 : BOOL; // 2nd light
  END_VAR
 
  METHOD PUBLIC OVERRIDE DAYTIME // The method 'DAYTIME' is overwritten.  
    LIGHT := FALSE;  // accessing the variable 'LIGHT' of the base function block "LIGHTROOM"
    LIGHT2 := FALSE; // an additional statement
  END_METHOD
 
  METHOD PUBLIC OVERRIDE NIGHTTIME // The method 'NIGHTTIME' is overwritten.   
    LIGHT := TRUE;  // accessing the variable 'LIGHT' of the base function block ""LIGHTROOM"
    LIGHT2 := TRUE; // an additional statement
  END_METHOD
  super(); // This calls the body of parent FB "LIGHTROOM" which itself calls the method "NIGHTTIME". In this case the "NIGHTTIME" implementation from the bottom-most base function block "LIGHT3ROOM" is taken.
  super.NIGHTTIME(); // This calls the method "NIGHTTIME" of the base function block "LIGHTROOM" directly. Here "LIGHT" is set to "TRUE".
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK LIGHT3ROOM EXTENDS LIGHT2ROOM (* the function block "LIGHT3ROOM" - now derived from "LIGHT2ROOM" *)
  VAR
    LIGHT3 : BOOL; // 3rd light
  END_VAR
 
  METHOD PUBLIC OVERRIDE DAYTIME
    LIGHT := FALSE;  // accessing the variable 'LIGHT' of the base function block "LIGHTROOM"
    LIGHT2 := FALSE; // accessing the variable 'LIGHT2' of the base function block "LIGHT2ROOM"
    LIGHT3 := TRUE;  // an additional statement
  END_METHOD
 
  METHOD PUBLIC OVERRIDE NIGHTTIME
    LIGHT := TRUE;   // accessing the variable 'LIGHT' of the base function block "LIGHTROOM"
    LIGHT2 := FALSE; // accessing the variable 'LIGHT2' of the base function block "LIGHT2ROOM"
    LIGHT3 := TRUE;  // an additional statement
  END_METHOD
  super();
END_FUNCTION_BLOCK

Example 4: Abstract function block and abstract method

INTERFACE ISwitch
  METHOD SwitchState
    VAR_INPUT
      STATE : BOOL;
    END_VAR
  END_METHOD
  METHOD SwitchOff
  END_METHOD
END_INTERFACE
 
FUNCTION_BLOCK ABSTRACT MySwitch IMPLEMENTS ISwitch (* the abstract function block = the base function block  *)
  VAR
    switchState : BOOL;
  END_VAR
  METHOD PUBLIC SwitchState
    VAR_INPUT
      STATE : BOOL;
    END_VAR
    switchState := STATE;
  END_METHOD
  METHOD PUBLIC ABSTRACT SwitchOff                (* the abstract method *)
  END_METHOD
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK MySwitch2 EXTENDS MySwitch  (* the non-abstract function block = the derived function block *)
    METHOD PUBLIC OVERRIDE SwitchOff              (* the method overwriting the abstract method of the abstract function block *)
    END_METHOD
END_FUNCTION_BLOCK
Example 5: Function block and method with "FINAL"
INTERFACE ISwitch
  METHOD SwitchState
    VAR_INPUT
      STATE : BOOL;
    END_VAR
  END_METHOD
  METHOD SwitchOff
  END_METHOD
END_INTERFACE
 
FUNCTION_BLOCK FINAL MySwitch IMPLEMENTS ISwitch  (* a function block that should NOT be used as a base function block - due to "FINAL" *)
  VAR
    switchState : BOOL;
  END_VAR
  METHOD PUBLIC SwitchState
    VAR_INPUT
      STATE : BOOL;
    END_VAR
    switchState := STATE;
  END_METHOD
  METHOD PUBLIC SwitchOff
  END_METHOD
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK MySwitch2 IMPLEMENTS ISwitch  (* a base function block *)
  VAR
    switchState : BOOL;
  END_VAR
  METHOD PUBLIC SwitchState
    VAR_INPUT
      STATE : BOOL;
    END_VAR
    switchState := STATE;
  END_METHOD
  METHOD PUBLIC FINAL SwitchOff                     (* a method that should not be overwritten - due to "FINAL" *)
  END_METHOD
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK MySwitch3 EXTENDS MySwitch    (* An error is reported for this derived function block. Reason: "MySwitch" is using "FINAL" and must not be used as base function block. *)
END_FUNCTION_BLOCK
 
FUNCTION_BLOCK MySwitch4 EXTENDS MySwitch2
  METHOD PUBLIC OVERRIDE SwitchOff           (* An error is reported for this method. Reason: The method "SwitchOff" in the base function block "MySwitch2" is using "FINAL" and must not be overwritten. *)
  END_METHOD
END_FUNCTION_BLOCK
Example 6: Methods with different visibility
FUNCTION_BLOCK ExampleVisibleMethods
  METHOD PUBLIC m1 : INT       // The method may be called from anywhere.
  END_METHOD
 
  METHOD PRIVATE m2            // The method may only be called from inside this function block "ExampleVisibleMethods".
  END_METHOD
 
  METHOD INTERNAL m3           // The method may be called from inside the same namespace.
  END_METHOD
 
  METHOD PROTECTED m4 : BOOL   // The method may only be called from inside this function block. Moreover, it may be called from functions blocks that are derived from "ExampleVisibleMethods".
  END_METHOD
END_FUNCTION_BLOCK

More examples for methods are specified under "Examples on using interfaces and variables based on these interfaces (incl. assignments)" and "Declaration of a function block in ST".