Syntax |
[Private | Public]
[Static] Function name[(arglist)] [As ReturnType] where arglist is a comma-separated list of the following (up to 30 arguments are allowed): [Optional] [ByVal | ByRef] parameter [()] [As type] |
||
Description |
Creates a user-defined function. |
||
Comments |
The Function statement has the following parts: |
||
|
Part |
Description |
|
|
Private |
Indicates that the function being defined cannot be called from other scripts. |
|
|
Public |
Indicates that the function being defined can be called from other scripts. If both the Private and Public keywords are missing, then Public is assumed. |
|
|
Static |
Recognized by the compiler but currently has no effect. |
|
|
Name |
Name of the function, which must follow Basic Control Engine naming conventions: |
|
|
|
The exclamation point (!) can appear within the name as long as it is not the last character, in which case it is interpreted as a type-declaration character.
|
|
|
|
Additionally, the name parameter can end with an optional type-declaration character specifying the type of data returned by the function (that is, any of the following characters: %, &, !, #, @). |
|
|
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 parameter is passed by value. |
|
|
ByRef |
Keyword indicating that 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 (for example, Integer, String, and so on). Arrays are indicated with parentheses. For example, an array of integers would be declared as follows: |
|
|
|
Function Test(a() As Integer) |
|
|
ReturnType |
Type of data returned by the function. If the return type is not given, then Variant is assumed. The ReturnType can only be specified if the function name (i.e., the name parameter) does not contain an explicit type-declaration character. |
|
|
A function returns to the caller when either of the following statements is encountered: End Function Functions can be recursive. |
||
|
Returning Values from Functions To assign a return value, an expression must be assigned to the name of the function, as shown below: Function TimesTwo(a As
Integer) As Integer |
||
|
If no assignment is encountered before the function exits, then one of the following values is returned: |
||
|
Value |
Data Type Returned by the Function |
|
|
0 |
Integer, Long, Single, Double, Currency |
|
|
Zero-length string |
String |
|
|
Nothing |
Object (or any data object) |
|
|
Empty |
Variant |
|
|
December 30, 1899 |
Date |
|
|
False |
Boolean |
|
|
The type of the return value is determined by the As ReturnType clause on the Function statement itself. As an alternative, a type-declaration character can be added to the Function name. For example, the following two definitions of Test both return String values: Function Test() As String Function Test$() |
||
|
Passing Parameters to Functions Parameters are passed to a function 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 function 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 function. 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 UserFunction: i = UserFunction(10,12,(j)) |
||
|
Optional Parameters The Basic Control Engine allows you to skip parameters when calling functions, as shown in the following example: Function Test(a%,b%,c%) As
Variant Sub Main You can skip any parameter with the following restrictions:
a = Test(1,,)
a =
Test(,1) '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 function, 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 function, 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 function. In this case, you can use the IsMissing function to determine if the parameter was skipped: Function Test(a,b,c) |
||
Example |
Function Factorial(n%) As Integer Sub Main() |
||
See Also |
Sub...End Sub (statement) |
F |