VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"Sub" Statement and Subroutine Call

This section describes how to define and invoke a subroutine procedure. There is no way to specify a return value in a subroutine procedure.

A subroutine procedure is similar to a function procedure. It can be defined with the "Sub" statement:

   Sub sub_name(argument_list)
      statement_block
   End Sub

where "sub_name" is the name of the sub procedure (subroutine), and "argument_list" a list of variables used to pass data into and/or out of the subroutine.

Of course, "argument_list" is optional.

Notice that subroutine does not return any values.

Invoking a subroutine is different than a function procedure. You can use one of the two syntaxes below:

1. Explicit call with a "Call" statement:

   Call sub_name(argument_list)

2. Implicit call without the keyword "Call" - Parentheses are not allowed if there are 2 or more argument in this syntax (very strange rule):

   sub_name argument
   sub_name(argument)
   sub_name argument_list 

Both syntaxes will cause the host environment to:

  • Stop execution in main code flow.
  • Map data or variables based on the argument list.
  • Execute the entire statement block defined inside the subroutine.
  • Continue to execute main code flow.

If you want terminate a sub procedure early, you can use the "Exit" statement:

   Exit Sub

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

"Function" and "Sub" Procedures

 What Is a Procedure?

 "Function" Statement and Function Call

 Function Procedure Example

"Sub" Statement and Subroutine Call

 Sub (Subroutine) Procedure Example

 Passing Arguments to Procedures

 Example - Passing Arguments by Reference

 Example - Passing Arguments by Value

 Passing Arrays as Arguments

 Variable Scope in Procedures

 Example - Variable Scope in Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"Sub" Statement and Subroutine Call - Updated in 2015, by Dr. Herong Yang