VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.01

Procedures - Functions and Subroutines

Part:   1   2  3  4  5  6 

VB Script Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Data Types and Literals


Logic Operations

String Operations

Conditional Statements


Loop Statements

Functions and Subroutines

Built-in Functions

Variable Inspection

... Table of Contents

This chapter describes:

  • What Is a Procedure?
  • Defining and Invoking Function Procedures
  • Function Procedure Example
  • Defining and Invoking Sub Procedures
  • Sub Procedure Example
  • Rules of Passing Arguments
  • Example - Passing Arguments by Reference
  • Example - Passing Arguments by Value
  • Passing Array as Arguments
  • Variable Scope in Procedures
  • Example - Variable Scopes

Notes and samples in this chapter are based Visual Basic 6.0.

What Is a Procedure?

A Procedure is a unit of code outside of the main execution code. But it can be executed by an invoking statement in the main execution code. There are 3 aspects about procedures:

1. Defining a procedure.

2. Invoking a procedure.

3. Exchanging data between the main execution code and a procedure.

VB offers two types of procedures:

1. Function Procedure - A procedure that returns a value explicitly.

2. Sub Procedure - A procedure that does not return any value explicitly.

Defining and Invoking Function Procedures

A "Function" statement defines a function procedure with the following syntax:

   Function function_name(argument_list)
      function_name = return_value
   End Function

where "function_name" is the name of the function, "argument_list" a list of variables used to pass data into and/or out of the function, and "return_value" is the value to be returned explicitly to the invoking statements.

Of course, "argument_list" is optional.

Assigning the return value to the function name is also optional. If not given, default value will be returned to the invoking statements. But this is not recommended.

Invoking a function procedure is simple, no need of any special statements. Just use the function name with an argument list in any expression:

   ... function_name(argument_list) ...

This will cause the system to:

  • Stop evaluating the expression.
  • Map data or variables based on the argument list.
  • Execute the entire statement block defined inside the function.
  • Take the value returned in the function name.
  • Continue to evaluate the expression.

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

   Exit Function

(Continued on next part...)

Part:   1   2  3  4  5  6 

Dr. Herong Yang, updated in 2006
VBScript Tutorials - Herong's Tutorial Notes - Procedures - Functions and Subroutines