Skip to main content

Object: Method

Symbol: _cds_icon_method.png

Keyword: METHOD

Methods are an extension of the IEC 61131-3 standard and a tool for object-oriented programming which is used for data encapsulation. A method contains a declaration and an implementation. However, unlike a function, a method is not an independent POU, and it is subordinated to a function block or program. A method can access all valid variables of the superordinate POU.

You can add a method below a program or a function block. Click Project → Add Object → Method. Then the Add Method dialog opens.

You can use interfaces for the organization of methods.

For more information, see: Implementation of an Interface

Important

When you copy a method below a POU and add it below an interface, or move the method there, the contained implementation is removed automatically.

Declaration

Syntax:

METHOD <access specifier> <method name> : <type of return value>

<access specifier>

Optional

Access specifier

Depending on the declared access specifier, a method can be called only within its own namespace (INTERNAL), only within its own POU and its derivatives (PROTECTED), or only within its own POU (PRIVATE). For PUBLIC, the method can be called from anywhere.

<method name>

Identifiers of methods

<type of retur value>

Optional

Return type of the method

Note: In the case of methods which do not have an explicit return type, the first declared output is used as the return value. A method does not return a value only if neither the return type nor the output are declared. However, a compiler error is not generated in either case.

Table 59. Possible scopes with variable declaration (parameter)

<scope list>

Scopes:

  • VAR_IN_OUT <variable declaration list> END_VAR

    For variable declaration for input/output variables

  • VAR_INPUT <variable declaration list> END_VAR

    For variable declaration for inputs

  • VAR_OUTPUT <variable declaration list> END_VAR

    For variable declaration for outputs

    Like functions, methods can have additional outputs. You also need to pass arguments (variables) to the additional outputs when you call the method.

  • VAR <variable declaration list> END_VAR

    For variable declaration for local purposes

<variable declaration list>

Variable declaration

<variable name> : <data type> := <initial value> ;

Semicolon-delimited list of variables (parameters) assigned to a scope according to their purpose.

  • Variables for input and input/output can have an initial value assigned to them

    The assignment of an initial value is optional. However, if one is specified, then passing an argument for this parameter can be omitted when calling the method. These kinds of parameters are called optional parameters.

  • The variables of a method (parameter) contain temporary data that are valid only during the execution of the method (stack variables). All variables that are declared and implemented in a method are reinitialized each time the method is called.



Example 517. Example

Declaration

METHOD PUBLIC DoIt : BOOL
VAR_INPUT
        iInput_1 : DWORD;
        iInput_2 : DWORD;
        sInput_3 : STRING(12);
END_VAR


Implementation

Optional

  • Access to function block instances or to program variables is allowed for the implementation of the method.

  • The THIS pointer allows for access to its own function block instance. Therefore, the pointer is allowed only in methods that are assigned to a function block.

  • A method cannot access VAR_TEMP variables of the function block.

  • A method can call itself recursively.

  • There is no implementation for interface methods

    Interface methods can have declared input, output, and VAR_IN_OUT variables, but do not contain an implementation.

Calling a Method

Syntax for calls:

<return value variable> := <POU name> . <method name> ( <argument passing> );

<return value variable>

Variable for the return value

The type has to match the return type of the method.

Note: In the case of methods which do not have an explicit return type, the first declared output is used as the return value. A method does not return a value only if neither the return type nor the output are declared. However, a compiler error is not generated in either case.

<POU name>

Identifier of the function block instance under which the method is arranged

<method name>

Identifiers of methods

<argument passing>

Comma-delimited list with the actual arguments

One argument is passed to each parameter (variable) of the method:

<parameter name> := <actual argument>

  • Each declared input/output or input is assigned the actual argument. The argument can be a value (literal), an expression, or a variable with matching type.

  • The actual argument (variable of the same type) is assigned to each declared output is assigned. The argument has to be a variable with the matching type.

  • Passing an argument for an input or an input/output can be omitted.

    Therefore, the number of arguments in the list may be less than the number of parameters (scope of input or input/output). In particular, if it is an optional parameter for which a default or initial value was specified in the declaration, then passing an argument can be omitted.

    Hint: If you get help from the Input Assistant when calling the method, then it will notify you about existing initial values.

  • It is optional to pass an argument with the specification of the parameter with name and assignment operator.

    Specifying only the argument is sufficient. The order of the variables in the declaration then determines which argument is passed to which parameter.

Example 518. Example

Declaration

METHOD PUBLIC DoIt : BOOL
VAR_INPUT
        iInput_1 : DWORD;
        iInput_2 : DWORD;
        sInput_3 : STRING(12);
END_VAR

Call with passing an argument to a parameter

bFinishedMethod := fbInstance.DoIt(sInput_3 := 'Hello World ', iInput_2 := 16#FFFF, iInput_1 := 16);

When the method is called, the return value of the method is assigned to a locally declared variable.



Example 519. Example

If you omit the names of the input variables, then the assignment of the arguments results from the declaration order.

Declaration

METHOD PUBLIC DoIt : BOOL
VAR_INPUT
        iInput_1 : DWORD;
        iInput_2 : DWORD;
        sInput_3 : STRING(12);
END_VAR
IF iInput_1 = iInput_2 THEN
	DoIt := TRUE; // explicit return value
END_IF

Call with passing an argument according to the order in the declaration

bFinishedMethod := fbInstance.DoIt( 16, 16#0010,'Hello World ');


Recursive method call

Within the implementation, a method can call itself, either directly by means of the THIS pointer, or by means of a local variable for the assigned function block.

  • <return value variable> := <POU name> . <method name> ( <argument passing> );

    Direct call of the relevant function block instance with the THIS pointer

  • <return value variable> := <POU name> . <method name> ( <argument passing> );

    Call by means of a local variable of the method that temporarily instantiates the relevant function block

A compiler warning is issued in the case of a recursive call. If the method is provided with the pragma {attribute 'estimated-stack-usage' := '<estimated stack size in bytes>'}, then the compiler warning is suppressed.

For an implementation example, see the "Attribute: 'estimated-stack-usage'" chapter.

To call methods recursively, it is not enough to specify only the method name. If only the method name is specified, then a compiler error is issued: Program name, function or function block instance expected instead of

Special methods of a function block

FB_Init

Declarations automatically implicit, but explicit declaration also possible

Contains initialization code for the function block, as is defined in the declaration part of the function block

FB_Reinit

Explicit declaration is necessary.

Call after the instance of the function block was copied (as during an online change). It reinitializes the new instance module.

FB_Exit

Explicit declaration is necessary.

Call for each instance of the function block before a new download or a reset or during an online change for all shifted or deleted instances.

Properties

Provides Set and/or Get accessor methods.

Dialog: Add Method

Function: Defines a method below the selected POU when the dialog is closed.

Call: Project → Add Object → Method; context menu

Requirement: A program (PRG) or a function block (FUNCTION_BLOCK) is selected in the POUs view or the Devices view.

Note

The interface of a method inserted below a basic function block is copied when a method with the same name is inserted below a derived function block.

Name

Example: meth_DoIt.

The standard methods FB_Init and FB_Exit are offered in a list box if they are not already inserted below the POU. If it is a derived function block, then the list box also offers all of the methods of the basic function block.

Return type

Default data type or structured data type of return value

Example: BOOL

Implementation language

Example: Structured Text (ST)

Access specifier

Controls access to data.

  • PUBLIC or not specified: Access is not restricted.

  • PRIVATE: Access is restricted to the program, function block, or GVL.

    The object is marked as (private) in the POU or device view. The declaration contains the keyword PRIVATE.

  • PROTECTED: Access is restricted to the program, function block, or GVL with its derivations. The declaration contains the keyword PROTECTED.

    The object is marked as (protected) in the POU or device view.

  • INTERNAL: Access to the method is restricted to the namespace (library).

    The object is marked as (internal) in the POU or device view. The declaration contains the keyword INTERNAL.

Abstract

standard icon: Identifies that the method does not have an implementation and the implementation is provided by the derived FB.

Add

Adds a new method to the selected object.

Input support when generating inheriting POUs

When you doing object-oriented programming and using the inheritance (EXTENDS keyword) of POUs, you can get support as follows:

When you insert an action, a property, a method, or a transition below a POU derived from a base POU, the Add … dialog opens. Then the input field for the name extends to a list box. The list box contains a valid selection from the actions, properties, methods, or transitions available in the base POU. Now you can, for example, easily accept a method of the base POU and then adapt it to the derived function of the POU.

Methods and properties with the access modifier PRIVATE are not listed here because they are also not inherited. Methods and properties with the access modifier PUBLIC automatically get a blank access modifier field when accepting into the derived POU, which means the same thing functionally.

For more information, see: Property, Method, Transition, Action,

Example 520. Example
_cds_img_input_wizzard_for_blocks.png