VBS Tutorial: VBScript basics-VBScript Process

Source: Internet
Author: User

VBScript Process

In VBScript, the process is divided into two types: Sub process and Function process.

Sub Process

SubThe process is included inSubAndEnd SubA group of VBScript statements between statements, which execute operations but do not return values.SubA process can use parameters (constants, variables, or expressions passed by the call process ). IfSubIf the process has no parametersSubThe statement must contain empty parentheses ().

The followingSubThe process uses two inherent (or built-in) VBScript functions, MsgBox and InputBox, to prompt users to enter information. The calculation result is displayed based on the information. ComputeFunctionThe process is completed. This process will be demonstrated after the following discussions.

 Sub ConvertTemp()    temp = InputBox("Enter the Fahrenheit temperature.", 1)    MsgBox "The temperature is " & Celsius(temp) & " Degree Celsius." End Sub
Function Process

FunctionThe process is included inFunctionAndEnd FunctionA group of VBScript statements between statements.FunctionProcess andSubThe process is similar,FunctionThe process can return values.FunctionA process can use parameters (constants, variables, or expressions passed by the call process ). IfFunctionIf the process has no parametersFunctionThe statement must contain empty parentheses ().FunctionThe process returns a value through the function name, which is assigned to the function name in the process statement.FunctionThe data type of the returned value is alwaysVariant.

In the following example, the Celsius function converts degrees Fahrenheit to degrees Celsius.SubWhen ConvertTemp calls this function, variables containing parameter values are passed to the function. The conversion result is returned to the call process and displayed in the message box.

 Sub ConvertTemp()     temp = InputBox("Enter the Fahrenheit temperature.", 1)     MsgBox "The temperature is " & Celsius(temp) & " Degree Celsius." End Sub Function Celsius(fDegrees)     Celsius = (fDegrees - 32) * 5 / 9 End Function
Process Data Entry and Exit

A parameter is used to transmit data to a process. The parameter is used as a placeholder for the data to be passed to the process. The parameter name can be any valid variable name. UseSubStatement orFunctionWhen creating a statement, the process name must be followed by parentheses. All parameters are included in the brackets, which are 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 obtain data from the process, you must useFunctionProcess. Remember,FunctionThe process can return values;SubThe process does not return values.

Use Sub and Function procedures in code

CallFunctionDuring the process, the function name must be used in the right side or expression of the variable assignment statement. For example:

 Temp = Celsius(fDegrees)

Or

 MsgBox "The temperature is " & Celsius(fDegrees) & " Degree Celsius."

CallSubDuring the process, you only need to enter the process name and all parameter values. The parameter values are separated by commas. You do not need to use the Call statement, but if you use this statement, you must include all parameters in brackets.

The following example shows two methods to call the MyProc process. One usageCallStatement; the other is not used. The two methods have the same effect.

 Call MyProc(firstarg, secondarg) MyProc firstarg, secondarg

Note thatCallWhen the statement is called, parentheses are omitted.

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.