Function

This statement declares the name, parameters, and code that form the body of a Function procedure.

Syntax

[Public | Private] Function name [(arglist)]
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression]
End Function

Parameters

name
Name of the Function; follows standard variable naming conventions.
arglist
List of variables that represent parameters passed to the Function procedure when it is called. Commas separate multiple variables.

The arglist parameter has the following syntax and parts:

[ByVal | ByRef] varname[( )]

ByVal
Indicates that the parameter is passed by value.
ByRef
Indicates that the parameter is passed by reference.
varname
Name of the variable that represents the parameter; follows standard variable naming conventions.
statements
Any group of statements to be executed within the body of the Function procedure.
expression
Return value of the Function.

Remarks

Public indicates that the Function procedure is accessible to all other procedures in all modules. Private indicates that the Function procedure is accessible only to other procedures in the module where it is declared. If not explicitly specified using either Public or Private, Function procedures are public by default; that is, they are visible to all other procedures in your module.

Function procedures can be recursive; in other words, they can call themselves to perform a specified task. However, recursion can lead to stack overflow.

To return a value from a function, assign the value to the function name. A function that returns an object reference returns Nothing if no object reference is assigned to name (using Set) within the Function.