Declares the name, arguments, and code that form the body of a Function procedure.
Syntax: [Public | Private | Friend] [Static] Function name [(arglist)] [As type]
[name = expression]
[name = expression] End Function
The Function statement syntax has these parts:
|Public||Optional. Indicates that the Function procedure is accessible to all other procedures in all modules. If used in a module that contains an Option Private, the procedure is notavailable outside the project.|
|Private||Optional. Indicates that the Function procedure is accessible only to other procedures in the module where it is declared.|
|Friend||Optional. Used only in a class module. Indicates that the Function procedure is visible throughout the project, but not visible to a controller of an instance of an object.|
|Static||Optional. Indicates that the Function procedure’s local variables are preserved between calls. The Static attribute doesn’t affect variables that are declared outside the Function,even if they are used in the procedure|
|Name||Required.Name of the Function; follows standard variable naming conventions.|
|arglist||Optional. List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas.|
|type||Optional. Data type of the value returned by the Function procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String, or (except fixed length), Object, Variant, or any user-defined type.|
|statements||Optional. Any group of statements to be executed within the Function procedure.|
|expression||Optional. Return value of the Function.|
The arglist argument has the following syntax and parts:
[Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [= defaultvalue]
Part Description Optional Optional. Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Optional can’t be used for any argument if ParamArray is used. ByVal Optional. Indicates that the argument is passed by value. ByRef Optional. Indicates that the argument is passed by reference. ByRef is the default in V.B. ParamArray Optional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. It may not be used with ByVal, ByRef, or Optional. varname Required. Name of the variable representing the argument; follows standard variable naming conventions. type Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported) Date, String (variable length only), Object, Variant, or a specific object type. If the parameter defaultvalue Optional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing.
If not explicitly specified using Public, Private, or Friend, Function procedures are public by default. If Static isn’t used, the value of local variables is not preserved between calls. The Friend keyword can only be used in class modules. However, Friend procedures can be accessed by procedures in any module of a project. A Friend procedure does’t appear in the type library of its parent class, nor can a Friend procedure be late bound.