Syntax |
[Private | Public]
[Static] Sub name[(arglist)] Where arglist is a comma-separated list of the following (up to 30 arguments are allowed): [Optional] [ByVal | ByRef] parameter[()] [As type] |
||
Description |
Declares a subroutine. |
||
Comments |
The Sub statement has the following parts: |
||
|
Part |
Description |
|
|
Private |
Indicates that the subroutine being defined cannot be called from other scripts. |
|
|
Public |
Indicates that the subroutine being defined can be called from other scripts. If the Private and Public keywords are both missing, then Public is assumed. |
|
|
Static |
Recognized by the compiler but currently has no effect. |
|
|
name |
Name of the subroutine, which must follow the Basic Control Engine naming conventions:
|
|
|
Optional |
Keyword indicating that the parameter is optional. All optional parameters must be of type Variant. Furthermore, all parameters that follow the first optional parameter must also be optional. If this keyword is omitted, then the parameter is required. Note: You can use the IsMissing function to determine if an optional parameter was actually passed by the caller. |
|
|
ByVal |
Keyword indicating that the parameter is passed by value. |
|
|
ByRef |
Keyword indicating that the parameter is passed by reference. If neither the ByVal nor the ByRef keyword is given, then ByRef is assumed. |
|
|
parameter |
Name of the parameter, which must follow the same naming conventions as those used by variables. This name can include a type-declaration character, appearing in place of As type. |
|
|
type |
Type of the parameter (i.e., Integer, String, and so on). Arrays are indicated with parentheses. For example, an array of integers would be declared as follows: Sub Test(a() As Integer) |
|
|
A subroutine terminates when one of the following statements is encountered: End Sub subroutines can be recursive. Passing Parameters to Subroutines Parameters are passed to a subroutine either by value or by reference, depending on the declaration of that parameter in arglist. If the parameter is declared using the ByRef keyword, then any modifications to that passed parameter within the subroutine change the value of that variable in the caller. If the parameter is declared using the ByVal keyword, then the value of that variable cannot be changed in the called subroutine. If neither the ByRef or ByVal keywords are specified, then the parameter is passed by reference. You can override passing a parameter by reference by enclosing that parameter within parentheses. For instance, the following example passes the variable j by reference, regardless of how the third parameter is declared in the arglist of UserSub: |
||
|
UserSub 10,12,(j) |
||
|
Optional Parameters The Basic Control Engine allows you to skip parameters when calling subroutines, as shown in the following example: Sub Test(a%,b%,c%) Sub Main You can skip any parameter with the following restrictions:
Test 1,,
|
||
|
Test ,1 'Only passes two out of three required parameters. Test 1,2 'Only passes two out of three required parameters. |
||
|
When you skip a parameter in this manner, the Basic Control Engine creates a temporary variable and passes this variable instead. The value of this temporary variable depends on the data type of the corresponding parameter in the argument list of the called subroutine, as described in the following table: |
||
|
Value |
Data Type |
|
|
0 |
Integer, Long, Single, Double, Currency |
|
|
Zero-length string |
String |
|
|
Nothing |
Object (or any data object) |
|
|
Error |
Variant |
|
|
December 30, 1899 |
Date |
|
|
False |
Boolean |
|
|
Within the called subroutine, you will be unable to determine if a parameter was skipped unless the parameter was declared as a variant in the argument list of the subroutine. In this case, you can use the IsMissing function to determine if the parameter was skipped: Sub Test(a,b,c) |
||
Example |
This example uses a subroutine to calculate the area of a circle. Sub Main() Sub PrintArea(r) |
||
See Also |
Main (statement); Function...End Function (statement). |
||
|
|
|
|
S |