Method

Overview

A method is a language element similar to a function that can be used in a context of a function block. It can be regarded as a function which contains an instance of the respective function block. Such as a function, a method has a return value, and its own declaration part for temporary variables and parameters.

Also as a means of object-oriented programming, you can use interfaces to organize the methods available in a project.

NOTE: When copying or moving a method or property from a POU to an interface, the contained implementations are deleted automatically. When copying or moving from an interface to a POU, you are requested to specify the desired implementation language.

Inserting a Method

To assign a method to a function block or interface, select the corresponding function block or interface node in the Applications tree, click the green plus button and execute the command Method. Alternatively, you can right-click the function block or interface node and execute the command Add Object > Method from the contextual menu.

In the Add Method dialog box, enter a Name, the desired Return Type, the Implementation Language, and the Access Specifier (see below). If the method does not have an implementation and the implementation is provided by the derived function block, select the option Abstract. For choosing the return data type, click the button ... to open the Input Assistant... dialog box.

Access specifier: For compatibility reasons, access specifiers are optional. The specifier PUBLIC is available as an equivalent for having set no specifier.

Alternatively, choose one of the options from the selection list:

  • PRIVATE: The access on the method is restricted to the function block.

  • PROTECTED: The access on the method is restricted to the function block and its derivation.

  • INTERNAL: The access on the method is restricted to the present namespace (the library).

Click Open to confirm. The method editor view opens.

Input Assistance when Creating Inheriting Function Blocks

EcoStruxure Machine Expert facilitates object-oriented programming using inheritance within function blocks: When you execute Add Object on a function block that inherits from another function block, the Action, Method, Property, and Transition elements used in the base function block are listed for selection:

  • Action, Method, Property, and Transition elements with Access specifier = PUBLIC, PROTECTED, and INTERNAL defined in the base function block are available for selection. You can adapt the definition for the inherited object. In the inherited object, the same Access specifier is assigned as to the source elements.

  • Action, Method, Property, and Transition elements with Access specifier = PRIVATE are not available for selection because access is restricted to the base function block.

Declaring a Method

Syntax:

METHOD <access specifier> <method name> : <return data type>VAR_INPUT ... END_VAR

For a description on how to declare interface handling methods, refer to the Interface chapter.

Calling a Method

Method calls are also named virtual function calls. For further information, refer to the chapter Method Invocation.

Note the following for calling a method:

  • The data of a method is temporary and only valid during the execution of the method (stack variables). Therefore, the variables and function blocks declared in a method are reinitialized at each call of the method.

  • Methods defined in an interface are only allowed to have input, output, and input/output variables, but no body (implementation part).

  • Methods such as functions can have additional outputs. They must be assigned during method invocation.

  • The declared access specifier defines how the method can be called:

    • INTERNAL: The method can be called within its own namespace.

    • PROTECTED: The method can be called within its own POU and its derivatives.

    • PRIVATE: The method can be called within its own POU.

    • PUBLIC: No restrictions apply for calling the method.

NOTE: If no return value is defined for the method, the value of the first output is returned. If no output is available for the method, a compiler error message is generated.

Implementing a Method

Note the following for implementing a method:

  • In the body of a method, access to the function block instance variables is allowed.

  • If necessary, use the THIS pointer which always points on the present instance.

  • VAR_TEMP variables of the function block cannot be accessed in a method.

  • A method can call itself recursively.

Calling a Method

Use the following syntax for calling a method:

<return value variable> := <POU name> . <method name> ( <method input name> := <variable name> (, <further method input name> := <variable name> )* );

Declaration example:

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

Call example:

bFinishedMethod := fbInstance.DoIt(sInput_3 :='Hello World ', iInput_2 := 16#FFFF,iInput_1 := 16);
NOTE: When the method is called, the return value of the method is assigned, for example, to variables declared locally. When you omit the names of the input variables, make sure to obey the declaration order.

Declaration example:

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

Call example:

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

Recursive Method Call

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

  • THIS^. <method name> ( <parameter transfer of all input and output variables>)

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

  • VAR fb_Temp : <function block name>; END_VAR

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

A recursive call results in a compiler message. The compiler message will not be issued if the method is provided with the pragma {attribute 'estimated-stackusage : '<estimated_stack_size_in_bytes>'}. Refer to the Attribute estimated-stack-usage chapter for an implementation example.

Thus, specifying the method name is insufficient for recursive method calls. In this case, the following compiler message is issued:

Program name, function or function block instance expected instead of
NOTE: Recursive calls are inherently difficult to implement and debug, and may lead to unwanted side effects such as memory issues and watchdog timeouts.
NOTICE
UNINTENDED APPLICATION ISSUES
  • Only implement recursive algorithms where necessary.
  • Be sure to have a solid understanding of recursive implementation techniques, and document well the supporting code.
Failure to follow these instructions can result in equipment damage.

Special Methods for a Function Block

Method

Description

Init

A method named FB_init is by default declared implicitly, but can also be declared explicitly. It contains initialization code for the function block as declared in the declaration part of the function block. Refer to FB_init method.

Reinit

If a method named FB_reinit is declared for a function block instance, it is called after the instance has been copied (like during Online Change) and will reinitialize the new instance module. Refer to FB_init, FB_reinit methods.

Exit

If an exit method named FB_exit is desired, it has to be declared explicitly. There is no implicit declaration. The Exit method is called for each instance of the function block before a new download, a reset or during online change for all moved or deleted instances. Refer to FB_exit method.

Properties and interface properties each consist of a Set and/or a Get accessor method.

Method Call Also When Application Is Stopped

In the device description file, it can be defined that a certain method should always be called task-cyclically by a certain function block instance (of a library module). If this method has the following input parameters, it is processed also when the active application is not running.

Example

VAR_INPUT
pTaskInfo : POINTER TO DWORD;
pApplicationInfo: POINTER TO _IMPLICIT_APPLICATION_INFO;
END_VAR

The programmer can check the application status via pApplicationInfo, and can define what should happen.

IF pApplicationInfo^.state = RUNNING THEN <instructions> END_IF