The VBScript Network and Systems Administrator's Cafe

Aug 1 2008   2:32AM GMT

VBScript Statements: Explanation of the Sub Statement



Posted by: Jerry Lees
Sub, Sub Statement, Subroutines, VBScript, VBScript Statements

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

Leave a comment: