Vbs basics-VBScript Process

Source: Internet
Author: User

In VBScript, the process is divided into two types: sub process and function process.

Sub Process

The sub process is a set of VBScript statements between sub and end sub statements. If the sub process has no parameters, the sub statement must contain empty parentheses (). The instance code is as follows:

1234 Call GetName() 'Call the sub processSub GetName()    MsgBox "I'm Sirrah" 'Output stringEnd Sub

Sub processes can use parameters (constants, variables, or expressions passed by the call process ). The instance code is as follows:

1234 Call GetName("I'm Sirrah") 'Call subSub GetName(name)    MsgBox name 'Output stringEnd Sub

 

 SubThe process has no returned value! Otherwise, an error is reported !!! The instance code is as follows:

1234 getName "Sirrah" 'Call ProcessSub getName(name)    getName = name 'Set the return valueEnd Sub

 Running the above script will result in the error shown. Please note that the process cannot return values.

Function Process

The function process is a set of VBScript statements between the function and end function statements. The function process is similar to the sub process. If the function process has no parameters, the function statement must contain empty parentheses (). The instance code is as follows:

1234 Call GetName() 'Call the FunctionFunction GetName()    MsgBox "I'm Sirrah" 'Output stringEnd Function

A function process can use parameters (constants, variables, or expressions passed by the call process ). The instance code is as follows:

1234 Call GetName("I'm Sirrah") 'Call the FunctionFunction GetName(name)    MsgBox name 'Output stringEnd Function

 FunctionThere is a returned value. A value is returned through the function name. This value is assigned to the function name in the statement of the process. The data type returned by function is always variant. The instance code is as follows:

1234 getName "Sirrah" 'Call the FunctionFunction getName(name)    getName = name 'Set the return valueEnd Function

 How to call the process

When calling a function, the function name must be used in the right side or expression of the variable assignment statement.

When calling the sub 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 of calling the process. One uses the call statement, and the other does not. The two methods have the same effect.

12345 getName "Sirrah" 'The first method of calling the processCall getName("Candy") 'Method 2 of the call ProcessSub getName(name)    MsgBox "I am"&nameEnd Sub


 Remember the differences between sub and function:

Sub has no return value, while function has a return value.

Sub cannot be placed in an expression, but function can.

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.