Declaration Permalink

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 616: 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

Declaration

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