Function statements under vbs (asp)

Source: Internet
Author: User

Function statement
Declare the name and parameters of the Function process and the code that constitutes the body.

[Public [Default] | Private] Function name [(
Arglist
)]
[Statements]
[Name = expression]
[Exit Function]
[Statements]
[Name = expression]
End Function parameters
Public
Indicates that the Function process can be accessed by all other processes in all scripts.
Default
It is used only with the Public keyword in the Class block to indicate that the Function process is the default method of the Class. If more than one Default process is specified in a class, an error occurs.
Private
It indicates that the Function process can only be accessed by other processes in the declared script or if the Function is a data class, the Function process can only be accessed by other processes in the class.
Name
Function name, which follows the standard variable naming rules.
Arglist
The list of variables that represent the parameters to be passed to the Function process during the call. Multiple variables are separated by commas.
Statements
Any statement group executed in the body of the Function process.
Expression
The Return Value of the Function.
The arglist parameter includes the following syntax and parts:

[ByVal | ByRef] varname [()]

Parameters
ByVal
This parameter is passed by value.
ByRef
This parameter is passed by reference.
Varname
Represents the name of the parameter variable; follows the standard variable naming rules.
Description
If Public or Private is not explicitly specified, the Function procedure is Public by default, that is, they are visible to all other procedures in the script. The value of the local variable in the Function is not retained during the call to the process.

Function procedures cannot be defined in any other process (such as Sub or Property Get.

You can use the Exit Function statement to Exit the Function immediately. The program continues to execute the statement that calls the Function process. Any Exit Function statement can appear at any position in the Function process.

Similar to the Sub process, the Function process is an independent process that can obtain parameters, execute a series of statements, and change their parameter values. The difference with the Sub process is that when you want to use the value returned by the Function, you can use the Function process on the right of the expression, which is the same as the use of internal functions, for example, Sqr, Cos, or Chr.

In an expression, you can call the Function process by using the Function name and then using parentheses to provide a list of corresponding parameters. For more information about calling a Function, see Call statements.

The warning Function process can be recursive, that is, the process can call itself to complete a given task. However, recursion may cause stack overflow.
To return a value from a function, you only need to assign the value to the function name. Any such assignment can appear at any position in the process. If no value is assigned to the name, a default value is returned. The value function returns 0, and the string function returns a zero-length string (""). If no object reference is specified to the name (Set) in the Function, the Function referenced by the object will return Nothing.

The following example shows how to assign a return value to a function named BinarySearch. In this example, False is assigned to the function name, indicating that no value is found.

Function BinarySearch (...)
...
'This value is not found. Returns False.
If lower> upper Then
BinarySearch = False
Exit Function
End If
...
The End Function uses two types of variables in the Function process: one is explicitly declared in the process, and the other is not. Variables explicitly declared in a process (using Dim or equivalent) are always local variables of the process. Variables used but not explicitly declared during the process are also local variables, unless they are explicitly declared at a higher level outside of the process.

The warning process can use variables that are not explicitly declared in the process, but any script-level defined names with the same name will conflict with the name. If the undeclared variables referenced in the process are the same as the names of other processes, constants, or variables, the process references the script-level names. To avoid such conflicts, use the Option Explicit statement to forcibly declare variables.
Warning VBScript may rearrange mathematical expressions to improve internal efficiency. When modifying the value of a variable in a mathematical expression, avoid using the Function in the same expression.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.