Subprocedures must have a Sub statement and corresponding End Sub statement. They can contain executable Visual Basic statements such as declaration and assignment statements. Subprocedures can take arguments such as variables, constants, and expressions. If no arguments are provided, the beginning Sub statement must contain an empty set of parentheses:

Public Sub DisplayCurrentTime() MsgBox "The time is " & Time End Sub

The next procedure implements adding two numbers, which are passed in as arguments.

Public Sub AddTwoNumbers(iNumber1 As Integer, iNumber2 As Integer)

MsgBox "The result of " & iNumberl & " and " & iNumber2 & _ " is " & iNumberl + iNumber2

End Sub

When executed by itself, the AddTwoNumbers procedure requires no parentheses to surround its parameter list:

AddTwoNumbers 4, 6

When used in an assignment statement, however, the comma-separated parameter list must be enclosed in parentheses:

lblOutput.Caption = AddTwoNumbers(4, 6)

Note again that subprocedures only execute statements and do not return a value to the calling procedure. If a return value is required, consider using a function procedure (discussed next).

0 0

Post a comment