Поделиться через


Sub Statement

Declares the name, arguments, and code that form the body of a Sub procedure.

[Public [Default] | Private] Sub name [(arglist)] 
   [statements]
   [Exit Sub]
   [statements]
End Sub

Arguments

  • Public
    Indicates that the Sub procedure is accessible to all other procedures in all scripts.

  • Default
    Used only with the Public keyword in a Class block to indicate that the Sub procedure is the default method for the class. An error occurs if more than one Default procedure is specified in a class.

  • Private
    Indicates that the Sub procedure is accessible only to other procedures in the script where it is declared.

  • name
    Name of the Sub; follows standard variable naming conventions.

  • arglist
    List of variables representing arguments that are passed to the Sub procedure when it is called. Commas separate multiple variables.

  • statements
    Any group of statements to be executed within the body of the Sub procedure.

The arglist argument has the following syntax and parts:

[ByVal | ByRef] varname[( )]

Arguments

  • ByVal
    Indicates that the argument is passed by value.

  • ByRef
    Indicates that the argument is passed by reference. If ByVal and ByRef are omitted, the default is ByRef.

  • varname
    Name of the variable representing the argument; follows standard variable naming conventions.

Remarks

Declaring and Calling Sub Procedures

You call a Sub procedure by using the procedure name followed by the argument list.

Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. Unlike a Function procedure, a Sub procedure does not return a value, so it cannot be used in an expression.

If not explicitly specified using either Public or Private, Sub procedures are public by default, that is, they are visible to all other procedures in your script.

You cannot define a Sub procedure inside any other procedure (e.g. Function or Property Get).

In a Sub declaration, each parameter can be specified as ByRef or ByVal. If neither is specified, the default is ByRef. If ByVal is specified, the corresponding argument is always passed by value when the subroutine is called. If ByRef (or neither) is specified, the argument can be passed by reference or by value when the subroutine is called. For more information, see ByRef and ByVal Parameters.

Warning

Sub procedures can be recursive, that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.

Local Variables in Sub Procedures

The values of local variables in a Sub procedure are not preserved between calls to the procedure.

Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local, unless they are explicitly declared at some higher level outside the procedure.

If variables are not explicitly declared in a procedure, a naming conflict can occur if anything you have defined at the script level has a duplicate name. If your procedure refers to an undeclared variable that has the same name as another procedure, constant or variable, it is assumed that your procedure is referring to that script-level name. To avoid this kind of conflict, use an Option Explicit Statement to force explicit declaration of variables.

Exit Sub Statement

The Exit Sub statement causes an immediate exit from a Sub procedure. Program execution continues with the statement that follows the statement that called the Sub procedure. Any number of Exit Sub statements can appear anywhere in a Sub procedure.

Example

The following example illustrates the use of the Sub statement.

ShowSum 5, 6

Sub ShowSum(value1, value2)
    Dim sum

    ' Determine and display the sum.
    sum = value1 + value2
    MsgBox "The sum is:  " & sum
End Sub

Requirements

Version 1

See Also

Concepts

ByRef and ByVal Parameters

Reference

Call Statement

Dim Statement

Exit Statement

Function Statement (VBScript)

Change History

Date

History

Reason

October 2009

Added information about ByVal and ByRef parameters.

Information enhancement.

October 2009

Added an example,

Customer feedback.

April 2009

Reorganized remarks.

Information enhancement.