Sub, Exit Sub, EndSub (Begin, Exit, End Subroutine)

The Sub declaration is used to mark the beginning of a subroutine in the program code. The Exit Sub statement specifies the condition under which the program should exit the subroutine. The EndSub statement is placed after the last instruction in the subroutine code to mark the end of the subroutine.

Syntax

Sub SubName [(ArgumentList )]

          [ statementblock ]

          [ Exit Sub ]

          [ statementblock ]

EndSub

Remarks

A Subroutine is a separate procedure that is called by the main program using a Call statement. A Subroutine can take arguments, perform a series of statements, and change the value of its arguments. The Scan/NextScan instruction loop can be used within a Subroutine to set a different execution interval than the main program.

Subroutines must be declared before they are called in the program. The code for a Subroutine cannot be contained within the code for another Subroutine; however, a Subroutine can be called by another Subroutine. Subroutines cannot be used in an expression.

Variables declared by Dim within a subroutine or function are local to that subroutine or function. The same variable name can be used within other subroutines or functions or as a global variable without conflict. Variables used as parameters to a subroutine or function are also local. The array length of a local variable in a subroutine will take on the array length of a global variable passed into the local variable. Note that variables initialized within a subroutine are initialized only at compile time. They are not re-initialized with each call to the subroutine.

Subroutines include the ability to pass in optional arguments. These optional parameters are preceded by the Optional keyword. Optional parameters must be intitialized using a constant value (required parameters cannot be initialized). The default for an optional parameter is used by passing in a comma; multiple default parameters are specified using multiple commas. Required parameters cannot follow optional parameters in the definition of the function.

Parameters

Sub

The Sub declaration identifies the beginning of a Subroutine.

Name

The name for the subroutine. The name is limited to 39 characters.

Type: Alphanumeric

Subroutine names follow the same rules that constrain the names of other variables. Because SubNames are recognized by all procedures in all modules, the name cannot be the same as any other globally recognized name in the program.

Arguments

The values to be passed to the subroutine (the variables can be Float, Long, or String; if not specified Float is assumed). The values are passed in the order listed.

Type: Constant, Variable, Array, or Expression

The use of an ArgumentList lets you assign variables within the main program to variables in the subroutine, process those variables in the subroutine, and then use the resulting processed values later in the program. Multiple variables are separated by commas. Changing an argument's value inside the Subroutine changes its value in the calling procedure. Use of ArgumentList is optional.

Statementblock

A statementblock is any group of statements that are executed within the body of the Subroutine.

Exit Sub

The Exit Sub statement causes an immediate exit from a Subroutine. Program execution continues with the instruction that follows the call to the Subroutine. Any number of Exit Sub statements can appear anywhere in a Subroutine. The Exit Sub statement is optional.

End Sub

The EndSub statement marks the end of a Subroutine.

Caution: Subroutines can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to strange results.