Sub...End Sub Statement

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

Syntax

Sub name ([arglist])
[statements]
[Exit Sub]
[statements]


End Sub

The Sub statement syntax has these parts:

Part Description
name Required. Name of the Sub; follows standard variable naming conventions.
arglist Optional. List of variables representing arguments that are passed to the Sub procedure when it is called. Multiple variables are separated by commas.
statements Optional. Any group of statements to be executed within the Sub procedure.

The arglist argument has the following syntax and parts:

[ByVal | ByRef] varname [As type] [=defval]

Part Description
ByVal Optional. Indicates that the argument is passed by value. ByVal is the default in ConceptDraw Basic.
ByRef Optional. Indicates that the argument is passed by reference.
varname Required. Name of the variable representing the argument being passed to the procedure; follows standard variable naming conventions.
type Optional. Data type of the valuepassed to the procedure; may be Byte, Boolean, Integer, Long, Single, Double, Date, String (except fixed length), Object , Variant or an object type.
defval

Optional. Constant that determine the value that will be passed to the procedure by default if this argument is omitted.

Remarks

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

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

    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. However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression.

    You call a Sub procedure using the procedure name followed by the argument list. See the Call statement for specific information on how to call Sub procedures.

    Variables used in Sub procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. 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.

    Note: You can't use GoSub, GoTo, or Return to enter or exit a Sub procedure.

Example

    This example uses the Sub statement to define the name, arguments, and code that form the body of a Sub procedure.

' Sub procedure definition.
' Sub procedure with two arguments.
Sub SubTraceXY(x As Double, y As Double)
   Trace "X = " & x & "  Y= " & y   ' Print x,y to Output window.
End Sub

 

See Also

Call Statement , Dim Statement , Exit Statement , Function Statement