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 and analyzed: 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 method_name [: return_type]
variable_declarations
statement_list
END_METHOD
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, as a statement, using the same dot notation as a structure member. Arguments are written positionally or by name, exactly as for a function block invocation:
instance_name.method_name(argument_list);
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.
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
END_FUNCTION_BLOCK
SetSpeed takes one parameter; Stop takes none. Both act on the
speed variable of the instance they are called on:
PROGRAM Main
VAR
motor : FB_Motor;
END_VAR
motor.SetSpeed(newSpeed := 1200);
motor.Stop();
END_PROGRAM
Current limitations¶
A return type is accepted on a declaration, but nothing can be done with it
yet. A method body cannot assign to the method’s own name the way a
function body does — that reports
P4007 — and a call is a
statement, not an expression, so x := instance.Method() is a syntax
error. Write methods without a return type until return values are
supported.
See Also¶
EXTENDS — derive from a base type and inherit its methods
THIS 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