Professional Documents
Culture Documents
Although this function example returns a value, not all functions have to re-
turn values. Functions, like subroutines, can perform actions without return-
ing any values.
Like procedures, functions can accept arguments. For example, to make our
addTwoNumbers function more versatile, we can rewrite it as follows:
Function addTwoNumbers2(num1 As Integer, num2 As Integer)
addTwoNumbers2 = num1 + num2
End Function
Now we can pass any two numbers to the preceding function to add them to-
gether. For example, we can write the following statement to display the result
of the function in a message box:
Sub DisplayResult()
MsgBox("Total=" & addTwoNumbers2(34,80))
End Sub
3. Event procedures
Event procedures are automatically executed in response to an event initiated
by the user or program code or triggered by the system. Events, event proper-
ties, and event procedures are introduced later in this chapter. They are also
covered in Chapter 9, “Getting to Know Built-In Tools for Testing and Debug-
ging.”
4. Property procedures
Property procedures are used to get or set the values of custom properties for
forms, reports, and class modules. The three types of property procedures
(Property Get, Property Let, and Property Set) begin with the Property key-
word followed by the property type (Get, Let, or Set), the property name, and a
pair of empty parentheses, and end with the End Property keywords. Here’s an
example of a property procedure that retrieves the value of an author’s royalty: