VBS TUTORIAL: VBScript statement-function statement _vbs

Source: Internet
Author: User
Tags naming convention

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

Parameters

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 aSub 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, aFunction procedure is an independent process that can get parameters, execute a series of statements, and change its parameter values. Unlike a Sub procedure, you can use a Function procedure on the right side of an expression when you want to use the value returned by a function, as in the case of an internal function, 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.

Be careful A 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. The return  value.    If lower > upper Then        BinarySearch = False              End If    . . .End Function

Variables used in Function procedures 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.

Be careful A Function procedure can be recursive, that is, the procedure can call itself to complete a given task. However, recursion can cause a stack overflow.

Be careful 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.

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.