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 for a method declared on the instance’s own type: a declaration is checked, and a call is resolved against the declared type of the instance, walking the EXTENDS chain to find the method. A call that the chain resolves to an inherited method is accepted by analysis but not yet compiled (see Current limitations). Enable with --allow-fb-inheritance; see Enabling Dialects and Features.

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. Arguments are written positionally or by name, exactly as for a function block invocation. 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.

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 passes analysis but reports P9999 when compiled. Code generation does not yet give a derived type storage for what it inherits, so there is nothing for the inherited body to run against. 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