Declaration of a method
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. Observe:
Possible keywords for a method
Use an optional keyword (= modifier) in order to define where the method is visible:
Moreover, you are able to specify the following keywords:
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. Deviations from IEC-standard For logi.CAD 3, the name of a method must be unique within the same →scope. T his is in contrast to the →IEC-standard where the →signature is used to unambiguously identify a method. |
A method behaves as a →function does.
You are able to use the following language elements for ST within a method:
Page:Call of method in ST
Page:CASE statement in ST
Page:EXIT statement in ST
Page:FOR statement in ST
Page:IF statement in ST
Page:Comments in ST
Page:Assignments in ST
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.
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
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
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
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
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
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".