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 alphabetic 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.
The arglist argument has the following syntax:

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

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)).

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.

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