METHOD¶
METHOD declares a named operation on a function block type. A method has
its own parameters and local variables, an optional return type, and a body,
and it runs against the variables of the instance it is called on. Methods
are what an INTERFACE describes and what a derived type inherits
through EXTENDS. A method declaration is terminated by
END_METHOD. Methods are part of the object-oriented programming
introduced in IEC 61131-3 Edition 3.
Note
METHOD is a keyword only when the --allow-fb-inheritance flag is enabled. When the flag
is not set, METHOD is an ordinary identifier and may be used as a
variable or type name, exactly as in standard IEC 61131-3. Pass --allow-fb-inheritance to
enable the object-oriented syntax, or select a dialect that includes it.
See Enabling Dialects and Features for the dialects and
flags reference.
Note
END_METHOD is the closing keyword of a method declaration and is
gated by the same flag. Like METHOD, it is an ordinary identifier
when the flag is not enabled.
IEC 61131-3 |
Edition 3 (object-oriented programming) |
Support |
Parsed, analyzed, compiled and executed: a declaration is checked,
and a call is resolved against the declared type of the instance,
walking the |
Syntax¶
A method is declared inside the function block it belongs to, between the
function block’s variable blocks and END_FUNCTION_BLOCK. The return type
is optional — a method without one is called for its effect rather than its
value:
METHOD [qualifiers] method_name [: return_type]
variable_declarations
[statement_list]
END_METHOD
The optional member qualifiers (such as
PRIVATE or PUBLIC FINAL) come before the name. The body may be
empty, and an ABSTRACT method has none.
Parameters are declared the same way as on a
function, with VAR_INPUT,
VAR_OUTPUT, and VAR_IN_OUT blocks; VAR declares locals of the
method itself.
A method is called on an instance using the same dot notation as a structure member. The call can be a statement, which discards any return value, or part of an expression, where it evaluates to the method’s return value:
instance_name.method_name(argument_list);
variable := instance_name.method_name(argument_list);
A method without a return type has no value to use, so calling it in an expression reports P4057.
The method to call is chosen from the declared type of the instance: the
type’s own methods first, then those of its EXTENDS base, and so on up
the chain.
Arguments bind as they do in a function block call (see
function block arguments), and are
evaluated in the order the method declares its parameters, with one
difference: a method keeps nothing between calls, so a call supplies every
VAR_INPUT and VAR_IN_OUT. An omitted one is
P4018, even when it declares an
initial value. A positional argument goes to a VAR_INPUT, never to a
VAR_IN_OUT, which is supplied by name. Calling a method that declares a
VAR_IN_OUT is not supported yet.
A method’s VAR_OUTPUT is stored with => after the method returns, as
a function’s is (see FUNCTION), and starts
from its initial value on every call:
motor.Measure(rpm => current_rpm);
Example¶
FUNCTION_BLOCK FB_Motor
VAR
speed : INT;
END_VAR
METHOD SetSpeed
VAR_INPUT
newSpeed : INT;
END_VAR
speed := newSpeed;
END_METHOD
METHOD Stop
speed := 0;
END_METHOD
METHOD IsRunning : BOOL
IsRunning := speed > 0;
END_METHOD
END_FUNCTION_BLOCK
SetSpeed takes one parameter; Stop takes none. Both act on the
speed variable of the instance they are called on. IsRunning returns
a value, so it can be used in a condition:
PROGRAM Main
VAR
motor : FB_Motor;
END_VAR
motor.SetSpeed(newSpeed := 1200);
IF motor.IsRunning() THEN
motor.Stop();
END_IF;
END_PROGRAM
Current limitations¶
A method with a STRING or WSTRING return type reports
P9999 when compiled.
A call to a method the instance’s type inherits through EXTENDS from a type that declares variables passes analysis but reports P9999 when compiled. Code generation does not yet give a derived type storage for the variables it inherits, so there is nothing for the inherited body to run against. A method inherited from a type without variables runs. Declare the method on the type being called, or call it on an instance of the type that declares it, until inherited storage is compiled.
See Also¶
EXTENDS — derive from a base type and inherit its methods
Member Qualifiers —
PUBLIC,PRIVATE,FINALand the other qualifiersTHIS and SUPER — the instance a method runs on, and its base type
INTERFACE — declare a set of method signatures
IMPLEMENTS — provide the methods declared by an interface
Object Oriented Programming — inheritance, interfaces, and abstract types explained
FUNCTION_BLOCK — the
FUNCTION_BLOCKunit