The VBScript Network and Systems Administrator's Cafe

Aug 1 2008   2:32AM GMT

VBScript Statements: Explanation of the Sub Statement

Jerry Lees Jerry Lees Profile: Jerry Lees

The VBScript statement Sub allows you to create a section of code that can be reused over and over, called a subroutine. It also allows you to name this section of code and refer to it by name, rather than typing it in multiple times.

Subroutines can optionally have parameters associated with them, but they must complete all their work in the subroutine because they can not return any values back to the portion of the program that called them.

Sub routines start with a declaration of the name and parameters and end with a “End Sub” statement. An example subroutine would look like so:

Sub AddNumbers (x, y)
    Wscript.Echo (x+y)
End Sub

To call this subroutine from your script you would simply place the following code elsewhere in your script:

AddNumbers 2, 4

(Notice that parenthesis were not used when calling the subroutine.)

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: