Function Statement (VBScript)
Declares the name, arguments, and code that form the body of a Function procedure.
[Public [Default] | Private] Function name [(arglist)] [statements] [name = expression] [Exit Function] [statements] [name = expression] End Function
Indicates that the Function procedure is accessible to all other procedures in all scripts.
Used only with the Public keyword in a Class block to indicate that the Function procedure is the default method for the class. An error occurs if more than one Default procedure is specified in a class.
Indicates that the Function procedure is accessible only to other procedures in the script where it is declared or if the function is a member of a class, and that the Function procedure is accessible only to other procedures in that class.
Name of the Function; follows standard variable naming conventions.
List of variables representing arguments that are passed to the Function procedure when it is called. Commas separate multiple variables.
Any group of statements to be executed within the body of the Function procedure.
Return value of the Function.
The arglist argument has the following syntax and parts:
[ByVal | ByRef] varname[( )]
Indicates that the argument is passed by value.
Indicates that the argument is passed by reference. If ByVal and ByRef are omitted, the default is ByRef.
Name of the variable representing the argument; follows standard variable naming conventions.
Declaring and Calling Functions
You call a Function procedure by using one of the following:
The function name, followed by the argument list.
The Call keyword, followed by the function name, followed by the argument list in parentheses. If you use the Call keyword, the function's return value is discarded.
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.
You cannot define a Function procedure inside any other procedure (for example, Sub or Property Get).
In a Function declaration, each parameter can be specified as ByRef or ByVal. If neither is specified, the default is ByRef. If ByVal is specified, the corresponding argument is always passed by value when the subroutine is called. If ByRef (or neither) is specified, the argument can be passed by reference or by value when the subroutine is called. For more information, see ByRef and ByVal Parameters.
Function procedures can be recursive, that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.
Visual Basic Scripting Edition (VBScript) may rearrange arithmetic expressions to increase internal efficiency. Avoid using a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression.
Function Return Values
You can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sqr, Cos, or Chr, when you want to use the value returned by the function.
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 a default value: a numeric function returns 0 and a string function returns a zero-length string (""). A function that returns an object reference returns Nothing if no object reference is assigned to name (using Set) within the Function.
Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure cannot be used in an expression.
Local Variables in Functions
The values of local variables in a Function are not preserved between calls to the procedure.
Variables that are explicitly declared in a procedure (using Dim or the equivalent) 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 some higher level outside the procedure.
For variables that are not explicitly declared in the procedure, a naming conflict can occur if anything you have defined at the script level has the same name. If your procedure refers to an undeclared variable that has the same name as another procedure, constant, or variable, it is assumed that your procedure is referring to that script-level name. To avoid this kind of conflict, use an Option Explicit Statement to force explicit declaration of variables.
Exit Function Statement
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.
The following example illustrates the use of the Function statement.
Dim retValue ' Call the function with and without the Call keyword. Call ShowSum(3, 4) ShowSum 5, 6 ' Omitting the Call keyword enables access ' to the function's return value. retValue = ShowSum(7, 8) MsgBox "The function returned: " & retValue Function ShowSum(value1, value2) Dim sum ' Determine and display the sum. sum = value1 + value2 MsgBox "The sum is: " & sum ' Set the function's return value. ShowSum = sum End Function
The following example illustrates the use of the Exit Function statement, which causes an immediate exit from a Function procedure.
Function BinarySearch(. . .) . . . ' Value not found. Return a value of False. If lower > upper Then BinarySearch = False Exit Function End If . . . End Function
Added information about ByVal and ByRef parameters.
Reorganized remarks and added an example.