Function statement
Declares the name of a Function procedure, its parameters, and the code that constitutes its body.
[Public [default]| Private] Function name [(
ArgList
)]
[Statements]
[NAME = expression]
[Exit Function]
[Statements]
[NAME = expression]
End Function Parameter
Public
Indicates that a Function procedure can be accessed by all other procedures in all scripts.
Default
Used only with the Public keyword in a class block to indicate that a Function procedure is the default method for a class. If more than one Default procedure is specified in a class, an error occurs.
Private
Indicates that a Function procedure can only be accessed by other procedures in the script in which it is declared, or if the function is a data class, then function procedures can only be accessed by other procedures in the class.
Name
The name of the Function, followed by a standard variable naming convention.
ArgList
Represents a list of variables to be passed to the parameters of a Function procedure at call time. Separate multiple variables with commas.
Statements
Any set of statements executed in the body of a Function procedure.
Expression
The return value of the Function.
The arglist parameter contains the following syntax and parts:
[ByVal | BYREF] varname[()]
Parameters
ByVal
Indicates that the parameter is passed in a value manner.
Byref
Indicates that the parameter is passed by reference.
VarName
Represents the name of a parameter variable, followed by a standard variable naming convention.
Description
If you do not explicitly specify to use public or Private, the Function procedure defaults to common, that is, they are visible to all other procedures in the script. The value of a local variable in a Function is not preserved in a call to a procedure.
You cannot define a Function procedure in any other procedure, such as a Sub or property get.
Use the Exit Function statement to exit immediately from a Function procedure. The statement after the program continues executing the statement that calls the Function procedure. Any Exit Function statement can appear anywhere in the Function procedure.
Like a Sub procedure, a Function procedure is an independent process that can get parameters, execute a series of statements, and change its parameter values. The difference from a Sub procedure is that when you want to use the value returned by a function, you can use a Function procedure on the right side of the expression, as in the way the internal function is used, such as SQR, Cos, or CHR.
In an expression, you can invoke a Function procedure by using the name of the functions and then giving the corresponding argument list with parentheses. For more information about calling a Function procedure, see the call statement.
A warning Function procedure can be recursive, that is, the procedure can call itself to complete a given task. However, recursion can cause a stack overflow.
To return a value from a function, simply assign the value to the function name. Any such assignment can occur at any point in the procedure. If you do not assign a value to name, the procedure returns a default value: The numeric function returns 0, and the string function returns a 0-length string (""). If no object reference in the function is assigned to name (using Set), the function that returns a reference to the object returns nothing.
The following sample shows how to assign a return value to a function named BinarySearch. In this example, False is assigned to the function name, indicating that a value was not found.
Function BinarySearch (...)
. . .
' The value was not found. Returns a value of False.
IF Lower > Upper Then
BinarySearch = False
Exit Function
End If
. . .
The End function uses variables in the Function procedure to fall into two categories: one is explicitly declared within a procedure, and the other is not. A variable that is explicitly declared within a procedure (using Dim or equivalent methods) is always a local variable of the procedure. Variables that are used but not explicitly declared in the procedure are also local variables, unless explicitly declared at a higher level outside the procedure.
A warning procedure can use a variable that is not explicitly declared within a procedure, but a name conflict arises whenever any script-level definition has the same name. If the undeclared variable referenced in the procedure is the same as the name of another procedure, constant, or variable, the procedure is assumed to refer to the script-level name. To avoid this type of conflict, use the Option Explicit statement to force an explicit declaration of a variable.
Warning VBScript may rearrange mathematical expressions to improve internal efficiency. When a Function procedure modifies the value of a variable in a mathematical expression, you should avoid using the function in the same expression.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.