Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of Function statement in VBS

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "the use of Function sentences in VBS". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Declare the name of the Function procedure, its parameters, and the code that forms its body.

[Public [Default] | Private] Function name [(

Arglist

)]

[statements]

[name = expression]

[Exit Function]

[statements]

[name = expression]

End Function

Parameter Public

Indicates that the Function procedure can be accessed by all other procedures in all scripts.

Default

Used only with the Public keyword in the Class block to indicate that the Function procedure is the default method of the class. If more than one Default procedure is specified in a class, an error occurs.

Private

Indicates that the Function procedure can only be accessed by other procedures in the script that declares it, or if the function is a data class, then the Function procedure can only be accessed by other procedures in that class.

Name

The name of the Function, following the standard variable naming convention.

Arglist

Represents a list of variables to pass to the Function procedure when called. Separate multiple variables with commas.

Statements

Any group of statements executed in the body of a Function procedure.

Expression

The return value of Function.

The arglist parameter contains the following syntax and sections:

[ByVal | ByRef] varname [()]

Parameter ByVal

Indicates that the parameter is passed by value.

ByRef

Indicates that the parameter is passed by reference.

Varname

Represents the name of the parameter variable; follows the standard variable naming convention.

Description

If you do not explicitly specify the use of Public or Private, Function procedures are public by default, that is, they are visible to all other procedures in the script. The value of a local variable in Function is not retained in the call to the procedure.

Function procedures cannot be defined in any other process (for example, Sub or Property Get).

Use the Exit Function statement to exit immediately from the Function procedure. The program continues to execute the statement after the statement that calls the Function procedure. Any Exit Function statement can appear anywhere in the Function procedure.

Similar to the Sub procedure, the Function procedure is an independent process that can take parameters, execute a series of statements, and change their parameter values. Unlike the Sub procedure, when you want to use the value returned by the function, you can use the Function procedure on the right side of the expression, in the same way as internal functions, such as Sqr, Cos, or Chr.

In an expression, you can call the Function procedure by using the function name, followed by a list of parameters in parentheses. For more information about calling Function procedures, see Call statements.

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

To return a value from a function, simply assign the value to the function name. Any such assignment can occur anywhere in the process. If no value is assigned to name, the procedure returns a default value: the numeric function returns 0 and the string function returns a zero-length string (""). If no object reference is assigned to name in Function (using Set), the function that returns the object reference returns 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 a value was not found.

Function BinarySearch (. . ). . . 'the value was not found. Returns the False value. If lower > upper Then BinarySearch = False Exit Function End If. . .End Function

The variables used in Function procedures fall into two categories: one is explicitly declared within the procedure, and the other is not. Variables that are explicitly declared within a procedure (using Dim or equivalent methods) are always local variables of the procedure. Variables that are used but are not explicitly declared in the procedure are also local variables unless they are explicitly declared at a higher level outside the procedure.

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

Be careful that VBScript may rearrange mathematical expressions to improve internal efficiency. When the Function procedure modifies the value of a variable in a mathematical expression, you should avoid using this function in the same expression.

This is the end of the introduction to "the use of Function sentences in VBS". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report