Function Statement
Declares the name, arguments, and code that form the body of a Function procedure.
Syntax
Function name [(arglist)]
[statements]
[name=expression]
[Exit Function]
[statements]
[name=expression]
End Function
Arguments:
-
Name: Name of the Function; follows standard variable naming conventions.
-
Arglist: List of variables representing arguments that are passed to the Function procedure when it is called. Commas separate multiple variables.
The arglist argument has the following syntax and parts:
[ByVal | ByRef] varname[( )]
Arglist Arguments:
-
ByVal: Indicates that the argument is passed by value.
-
ByRef: Indicates that the argument is passed by reference.
-
Varname: Name of the variable representing the argument; 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
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 script. The value of local variables in a Function is not preserved between calls to the procedure.
You cannot define a Function procedure inside any other procedure (e.g. Sub or Property Get).
The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement that follows the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure.
Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Functions Sqr, Mid, or Chr, when you want to use the value returned by the function.
You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression. See the Call statement for specific information on how to call Function procedures.
To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns nothing.
The following example shows how to assign a return value to a function named
FindValue
. In this case, False is assigned to the name to
indicate that some value was not found.
Example 1:
Function FindValue(val)
Dim notFound
notFound = True
'Business Logic
'val not found. Return a val of False.
If notFound Then
FindValue = False
Exit Function
End If
' Business Logic
End Function
'Usage:-
val = FindValue(8)
Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at the highest level outside the procedure.