This statement begins a user-defined subroutine procedure. It specifies any parameters that are passed when it is called.
[ Public | Private | Shared ] Subsubroutine_name([parameter_list])
Prerequisites
Parameters
subroutine_name
The name of the subroutine to be defined.
parameter_list
A list of parameters that are passed to the procedure when it is called. Each parameter appears as a locally defined variable and is associated with a value when the procedure is called. The caller must provide arguments that match the number and type of the parameters specified in this statement. The list may be empty if the subroutine has no parameters. Multiple parameter list elements are separated by “,’. Each element has the form:
[ ByVal | ByRef ] parameter_nameAstype
parameter_name
The name of the variable associated with this parameter. This name is known only within the procedure being defined.
type
The type of this parameter. The type may be either a primitive type or the name of a built-in class. The primitive type keywords are:
Boolean, Byte, Double, Integer, Short, Single
If a class name is specified, the variable becomes an object variable.
Either ByVal or ByRef can be specified, but not both. If neither is specified, the default is ByVal. A ByVal parameter receives a copy of argument value from the caller. The local procedure can change the value without affecting the caller’s value. A ByRef parameter references the caller’s value directly. Any changes to a ByRef parameter in the called routine are reflected in the calling routine. Since object variables always deal with pointers to object values, the called routine can always change an object value, even when passed using a ByVal parameter.
Remarks
A Sub procedure does not return a value and cannot be used within an expression. A Sub procedure can be used with a Call statement or by itself as a statement. A Sub definition must always end with an End Sub statement. A subroutine procedure exits when it encounters the End Sub statement, an Exit Sub statement, or a Return statement. If Public is specified, this procedure can be called from other modules or classes. Otherwise it can only be called from the module or class where it is defined. The Shared keyword can only be used within a class definition. If it appears, the subroutine is associated with the entire class rather than with a particular object of that class type.
Examples
Sub add_sub (x As Integer, y As Integer, ByRef result As Integer)
result = x+y
End Sub
add_sub(4, 5, a) ' Variable a gets value 9
See Also
Statements |Delegate Statement | End Sub Statement | Exit Sub Statement | Return Statement | Sub Statement