Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Sub Procedures

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. Each time the procedure is called, its statements are executed, starting with the first executable statement after the Sub statement and ending with the first End Sub, Exit Sub, or Return statement encountered.

A Sub procedure performs actions but does not return a value. It can take arguments, such as constants, variables, or expressions, that are passed to it by the calling code.

The syntax for declaring a Sub procedure is as follows:

[accessibility] Sub subname[(argumentlist)]
   ' Statements of the Sub procedure go here.
End Sub

The accessibility can be Public, Protected, Friend, Protected Friend, or Private.

You can define Sub procedures in modules, classes, and structures. They are Public by default, which means you can call them from anywhere in your application.

Argument Declaration

You declare each argument for a procedure the same way you declare a variable, specifying the argument name and data type. You can also specify the passing mechanism, and whether the argument is optional.

The syntax for each argument in the argument list is as follows:

[Optional] [ByVal|ByRef] [ParamArray] argumentname As datatype

If the argument is optional, you must also supply a default value in its declaration, as follows:

Optional [ByVal|ByRef] argumentname As datatype = defaultvalue

Calling Syntax

You invoke a Sub procedure explicitly with a stand-alone calling statement. You cannot call it by using its name within an expression. The calling statement must provide values for all arguments that are not optional, and it must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The use of the Call keyword is optional also.

The syntax for a call to a Sub procedure is as follows:

[Call] subname[(argumentlist)]

The following Sub procedure tells the computer operator which task the application is about to perform, and also displays a time stamp. Instead of duplicating this code at the beginning of every task, the application simply calls TellOperator from various places. Each call passes a string in the Task argument that identifies the task being started.

Sub TellOperator(ByVal Task As String)
   Dim Stamp As Date      ' Stamp is local to TellOperator.
   Stamp = TimeOfDay()    ' Get current time for time stamp.
   ' Use MessageBox class of System.Windows.Forms namespace.
   MessageBox.Show("Starting " & Task & " at " & CStr(Stamp))
End Sub

A typical call to TellOperator is as follows:

Call TellOperator("file update")

See Also

Procedures | Event-Handling Procedures | Function Procedures | Property Procedures | Procedure Arguments | Sub Statement

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft