This statement begins a user-defined function procedure. It specifies the function return data type and any parameters that are passed when it is called.
[ Public | Private | Shared ] Functionfunction_name([parameter_list]) As type
Prerequisites
Parameters
function_name
The name of function to be defined.
parameter_list
A list of parameters that are passed to the procedure when it is called. Each parameter appears as a locally defined variable and is associated with a value when the procedure is called. The caller must provide arguments that match the number and type of the parameters specified in this statement.
The list may be empty if the function has no parameters. Multiple parameter list elements are separated by ",". Each element has the form:
[ ByVal | ByRef ] parameter_nameAstype
parameter_name
The name of the variable associated with this parameter. This name is known only within the procedure being defined.
type
The type of this parameter. The type may be either a primitive type or the name of a built-in class. The primitive type keywords are:
Boolean, Byte, Double, Integer, Short, Single
If a class name is specified, the variable becomes an object variable.
Either ByVal or ByRef can be specified, but not both. If neither is specified, the default is ByVal. A ByVal parameter receives a copy of argument value from the caller. The local procedure can change the value without affecting the caller’s value. A ByRef parameter references the caller’s value directly. Any changes to a ByRef parameter in the called routine are reflected in the calling routine.
Since object variables always deal with pointers to object values, the called routine can always change an object value, even when passed using a ByVal parameter.
type
The type of the value returned by this function. The type may be a primitive type, the name of a built-in class, or the name of a user-defined class. The primitive type keywords are:
Boolean, Byte, Double, Integer, Short, Single
If a class name is specified, the returned type is an object.
Remarks
A Function procedure returns a value that can be used within an expression where a value of the proper type is allowed. A Function can also be used with a Call statement or by itself as a statement when the returned value is not needed.
A Function definition must always end with an End Function statement.
A Function procedure exits when it encounters the End Function statement, an Exit Function statement, or a Return statement.
The returned value of function is specified by assigning a value to a variable named function_name, or by a Return statement.
If Public is specified, this procedure can be called from other modules or classes. Otherwise it can only be called from within the module or class where it is defined.
The Shared keyword can only be used within a class definition. If it appears, the Function is associated with the entire class rather than with a particular object of that class type.
Examples
Function add_function (x As Integer, y As Integer) As Integer
add_function = x+y
End Function
a = add_function(4, 5) * 2 ' Variable a gets value 18
See Also
Statements | Delegate Statement | End Function Statement | Exit Function Statement | Return Statement | Sub Statement