VBScript Tutorial Nineth lesson VBScript process _vbs

Source: Internet
Author: User
VB Tutorials > VBScript process
Process classification
In VBScript, processes are divided into two categories: Sub processes and Function processes.
Sub procedure
A Sub procedure is a set of VBScript statements that are contained between a sub and an End Sub statement, performing an action but not returning a value. Sub procedures can use parameters (constants, variables, or expressions passed by the calling procedure). If the Sub procedure has no arguments, the sub statement must contain an empty bracket ().
The Sub procedure below uses two intrinsic (or built-in) VBScript functions, namely MsgBox and InputBox, to prompt the user for information. It then displays the results calculated based on this information. The calculation is completed by a Function procedure created using VBScript. This process is demonstrated after the following discussion.

Sub converttemp ()
temp = InputBox ("Please enter Fahrenheit temperature.") ", 1)
MsgBox "Temperature" & Celsius (temp) & Celsius. "
End Sub

Function procedure
A Function procedure is a set of VBScript statements that are contained between a function and an End Function statement. A Function procedure is similar to a Sub procedure, but a Function procedure can return a value. A Function procedure can use parameters (constants, variables, or expressions passed by the calling procedure). If the Function procedure has no arguments, the function statement must contain an empty bracket (). A Function procedure returns a value that is assigned to a function name in the statement of a procedure by its name. The data type of a Function return value is always a Variant.
In the following example, the Celsius function converts Fahrenheit to degree Celsius. Sub procedure ConvertTemp When this function is called, the variable containing the parameter value is passed to the function. The conversion result is returned to the calling procedure and displayed in a message box.

Sub converttemp ()
temp = InputBox ("Please enter Fahrenheit temperature.") ", 1)
MsgBox "Temperature" & Celsius (temp) & Celsius. "
End Sub

Function Celsius (fdegrees)
Celsius = (fDegrees-32) * 5/9
End Function

Process data entry and exit
The way to pass data to a process is to use parameters. The parameter is used as a placeholder for the data to be passed to the procedure. The parameter name can be any valid variable name. When you create a procedure by using a SUB statement or Function statement, the procedure name must be followed by parentheses. The parentheses contain all the parameters, separated by commas. For example, in the following example, Fdegrees is a placeholder for the value passed to the Celsius function:
Function Celsius (fdegrees)
Celsius = (fDegrees-32) * 5/9
End Function

To get data from a procedure, you must use a Function procedure. Keep in mind that a Function procedure can return a value, and a Sub procedure does not return a value.
Using Sub and Function procedures in your code
When calling a Function procedure, the function name must be used at the right end of the variable assignment statement or in an expression. For example:

Temp = Celsius (fdegrees)
Or
MsgBox "Temperature is" & Celsius (fdegrees) & "Celsius." "


When you call a Sub procedure, you simply enter a procedure name and all parameter values, separated by commas. You do not need to use the call statement, but if you use this statement, you must enclose all parameters in parentheses.
The following example shows two ways to invoke the MyProc procedure. One uses the call statement, and the other is not used. The results are the same in both ways.

Call MyProc (Firstarg, Secondarg)
MyProc Firstarg, Secondarg

Note that parentheses are omitted when a call statement is not used.

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.