Function Statement |
[ ClassBehavior ][ AccessModifier ] Function name _ [( arglist )] [As type ][( )] [ statements ] [ name = expression ] [ statements ] End Function
One of the following keywords:
Indicates that more than one declaration of this function exists (with different argument signatures). For more detail, see Chapter 4.
For derived classes, indicates that the function overrides the function by the same name (and argument signature) in the base class. For more detail, see Chapter 4.
Indicates that the function can be overridden in a derived class. For more detail, see Chapter 4.
Indicates that the function cannot be overridden in a derived class. For more detail, see Chapter 4.
Indicates that the function must be overridden in a derived class. For more detail, see Chapter 4.
In a derived class definition, indicates that this element shadows any elements of the same name in the base class.
A shared function is callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static functions.
One of the following keywords: Public , Private , Protected , Friend , Protected Friend . The following table describes the effects of the various access modifiers. Note that direct access refers to accessing the member without any qualification, as in:
classvariable = 100
and class/object access refers to accessing the member through qualification, either with the class name or the name of an object of that class:
Direct access scope | Class/object access scope | |
---|---|---|
Private | Declaring class | Declaring class |
Protected | All derived classes | Declaring class |
Friend | Derived in-project classes | Declaring project |
Protected Friend | All derived classes | Declaring project |
Public | All derived classes | All projects |
For more information, see Section 4.7 in Chapter 4.
The name of the function.
A comma-delimited list of variables to be passed to the function as arguments from the calling procedure.
arglist uses the following syntax and parts :
[Optional] [ByVal ByRef] [ParamArray] varname [( )] [As type ] _ [= defaultvalue ]
An optional argument is one that need not be supplied when calling the function. However, all arguments following an optional one must also be optional. A ParamArray argument cannot be optional.
The argument is passed by value; that is, the local copy of the variable is assigned the value of the argument.
The argument is passed by reference; that is, the local variable is simply a reference to the argument being passed. All changes made to the local variable will be also reflected in the calling argument. ByVal is the default method of passing variables.
Indicates that the argument is an optional array of Objects (or a strongly typed array, if Option Strict is on) containing an arbitrary number of elements. It can only be used as the last element of the argument list and cannot be used with the ByRef or Optional keywords.
The name of the local variable containing either the reference or value of the argument.
The data type of the argument.
For optional arguments, you must specify a default value.
The return data type of the function.
Program code to be executed within the function.
The value to return from the function to the calling procedure.
Defines a function procedure
Overloads and Shadows cannot be used in the same declaration.
Functions cannot be nested; that is, you cannot define one function inside another function. (This applies to all procedures.)
If you do not include one of the access keywords, a function will be Public by default.
Any number of Exit Function statements can be placed within the function. Execution will continue with the line of code immediately following the call to the function. If a value has not been assigned to the function when the Exit Function statement executes, the function will return the default initialization value of the data type specified for the return value of the function. If the data type of the function was an object reference, the exited function will return Nothing .
The return value of a function is passed back to the calling procedure by either assigning a value to the function name or by using the Return statement. However, the Return statement also exits the function, whereas assigning the return value to the function name does not exit the function.
To return arrays of any type from a procedure, you must use parentheses after the data type in the return value of the function declaration, as in:
Public Function Test() As Integer( )
If you specify an optional parameter in your function declaration, you must also provide a default value for that parameter. For example:
Private Function ShowMessage(Optional sMsg _ As String = "Not given")
There is often confusion between using the ByRef and ByVal methods to assign arguments to a function. ByRef assigns a reference of the variable in the calling procedure to the variable in the function; any changes made to the variable from within the function are in reality made to the variable in the calling procedure. On the other hand, ByVal assigns the value of the variable in the calling procedure to the variable in the function. Changes made to the variable in the function have no effect on the variable in the calling procedure. In general, ByRef arguments within class modules take longer to perform, since marshaling back and forth between function and calling module must take place; so unless you explicitly need to modify a variable's value within a function, it's best to pass parameters by value.
Since a variable passed to a function by reference is actually modified by the function, you can use such variables to "return" multiple values from the function.
If a parameter array is used in VB 6, it is a comma-delimited list of values in the calling procedure that is treated as an array of variants in the called function. In VB.NET, the arguments can be any data type, and they can be either a comma-delimited list of scalar values or an array.
In VB 6, the elements in parameter arrays are passed by reference; in VB.NET, they are passed by value.
If you do not specify whether an individual element in arglist is passed ByVal or ByRef , it is passed by reference in VB 6. In VB.NET, it is passed by value.
In VB 6, you can call a function that has arguments in a number of ways:
x = SomeFunction(arg1, arg2) Call SomeFunction(arg1, arg2) SomeFunction arg1, arg2
In VB.NET, parentheses are required in the function call:
x = SomeFunc(arg1, arg2) Call SomeFunc(arg1, arg2) SomeFunc(arg1, arg2)
In VB 6, optional arguments do not require that you specify a default value. Instead, the IsMissing function is used to determine whether the optional argument is supplied (although in some cases it is unreliable). In VB.NET, you must assign a default value to an optional argument.
Sub Statement