Function...Fend Statement

A function is a group of program statements which includes a Function statement as the first statement and an Fend statement as the last statement.

Syntax
Function funcName [(argList)] [As type(function)]
statements
Fend

Parameters

funcName
The name which is given to the specific group of statements bound between the Function and Fend instructions. The function name must contain alphanumeric characters and may be up to 64 characters in length. Underscores are also allowed.
argList
List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas. These are optional.
The arglist argument has the following syntax:

[ {ByRef | ByVal}​ ] varName [( )] As type(argument)

Specifies how variables are passed as ByRef or ByVal. If omitted, ByVal is assumed to be specified.

ByRef
Specify ByRef to refer to a variable of the function to be called. In this case, the argument change in a function can be reflected to the variable of the calling side.
ByVal
Optional. Specify ByVal when you do not want any changes in the value of the variable to be seen by the calling function. This is the default.
varName [()]
The variable name of the argument, which is a required parameter. Name of the variable representing the argument; follows standard variable naming conventions. If you use an array variable as argument, you should specify ByRef and add empty parentheses “()” representing the array after the variable name.
As type(argument)
This parameter is required. You must declare the type of argument.
As type(function)
Use this parameter if you want to obtain return values. You must declare the type of return values.

Return Values
Value whose data type is specified with the As clause at the end of the function declaration (As type(function)). If not specified, 0 will be returned.

Description
The Function statement indicates the beginning of a group of SPEL+ statements. To indicate where a function ends we use the Fend statement. All statements located between the Function and Fend statements are considered part of the function.

The Function...Fend combination of statements could be thought of as a container where all the statements located between the Function and Fend statements belong to that function. Multiple functions may exist in one program file.

If you want to use the return value, assign the value to the variable name which has the same name as the function and then terminate the function.

Functions can also be specified as an argument of a command. The function will be executed as a subroutine, and the return value will be used as the value for the argument.

Note


When writing a task identifier to an argument of a command such as the Quit command, do not enclose the function name in parentheses or include any arguments. Otherwise, the return value will be specified instead of the task name.


See Also
Call, Fend, Halt, Quit, Return, Xqt

Function...Fend Statement Example
[Example 1]
The following example shows 3 functions which are within a single file. The functions called task2 and task3 are executed as background tasks while the main task called main executes in the foreground.

Function main
  Xqt 2, task2   'Execute task2 in background
  Xqt 3, task3   'Execute task3 in background
  .
  .
  .
Fend

Function task2
  Do
    On 1
    On 2
    Off 1
    Off 2
  Loop
Fend

Function task3
  Do
    On 10
    Wait 1
    Off 10
  Loop
Fend

[Example 2]
In the following example, the pressure control sequence for peripherals is supplied as an argument and the result sent to the external device is displayed as a return value.

Function main
  Integer iResult
  Real Sequence1(200)
  .
  .
  iResult = PressureControl(ByRef Sequence1())   'Argument is array
  .
  Print "Result:", iResult
  .
Fend

Function PressureControl(ByRef Array1() As Real) As Integer
  .
  (Control pressure for peripherals according to Array1)
  .
  PressureControl = 3    'Return value
  .
  .
Fend

[Example 3]
The following is an example showing the differences in how a function can be called.

Function main

    ' A function with no return value or argument definition
    Print TaskInfo(SubFunction0, 0)       ' Get information about task name "SubFunction0."
    Print TaskInfo((SubFunction0), 0)     ' After executing it as a subroutine, the return value "0" is out of range and an error occurs.

    ' Functions with defined return values
    Print TaskInfo(SubFunction1, 0)       ' Get information about task name "SubFunction0."
    Print TaskInfo((SubFunction1), 0)     ' After executing as a subroutine, gets the task number information corresponding to the return value.

    ' A function that has both a return value and arguments defined
    Print TaskInfo(SubFunction2, 0)       ' Get information about task name "SubFunction2."
    Print TaskInfo(SubFunction2(0), 0)    ' After executing as a subroutine, gets the task number information corresponding to the return value.

    ' A function defined to return a string
    Print TaskInfo(SubFunction1$, 0)      ' Get information about task name "SubFunction1$."
    Print TaskInfo((SubFunction1$), 0)    ' A build error occurs because the data type of the first argument is different.
Fend

Function main2

    ‘ Function that does not have a return value or argument that is defined
    Quit SubFunction0                  ' Stops the task named “SubFunction0”.
    Quit (SubFunction0)                ' After executing as a subroutine, the return value zero will be out of range and an error will occur. 、

    ‘ Function that has a return value defined
    Quit SubFunction1                  ' Stops the task named “SubFunction1”.
    Quit (SubFunction1)                ' After executing it as a subroutine, the task number corresponding to the return value is stopped.

    ' A function that has both a return value and arguments defined
    Quit SubFunction2                  ' Stops the task named “SubFunction2”.
    Quit (SubFunction2)                ' If 0 is passed as an argument to the subroutine, the return value corresponds to the task number that will be stopped.

    ' Function which has the a return value defined in strings
    Quit SubFunction1$                  ' Stops the task named “SubFunction1$”.
    Quit (SubFunction1$)                ' Build error occurs because the argument’s data type is different.
Fend

Function SubFunction0
    ・
Fend

Function SubFunction1 As Integer
    ・
    SubFunction1 = 1
Fend

Function SubFunction2(i As Integer) As Integer
    ・
    SubFunction2 = 1
Fend

Function SubFunction1$ As String
    ・
    SubFunction1$ = "main"
Fend