Microsoft Visual Basic
2012: Reloaded
Fifth Edition
Chapter Eight
Sub and Function Procedures
Objectives
After studying this chapter, you should be able to:
• Create and call an independent Sub procedure
• Pass data to a procedure
• Explain the difference between passing data by
value and passing data by reference
• Code the CheckedChanged event procedure
• Desk-check an application’s code
• Associate a procedure with more than one object
and event
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
2
Objectives
(cont'd.)
• Explain the purpose of the sender and e
parameters
• Explain the difference between Sub and Function
procedures
• Create and invoke a Function procedure
• Convert an Object variable to a different type
using the TryCast operator
• Utilize a timer control
3
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Sub Procedures
• There are two types of Sub procedures in Visual Basic:
• Event procedures and Independent Sub procedures
• Event procedures are Sub procedures that are
associated with a specific object and event, such
as a button’s Click event or a text box’s
TextChanged event
• An independent Sub procedure, is a procedure that
is independent of any object and event
• Processed only when called (invoked) from code
• Invoked by using the Call statement
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
4
Sub Procedures
(cont'd.)
Figure 8-1: How to create and call an independent Sub procedure
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
5
Sub Procedures
(cont'd.)
Figure 8-2: Lanza
Trinkets application
6
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Including Parameters in an
Independent Sub Procedure
• A parameter is a memory location that stores an
item of data passed to the procedure when the
procedure is invoked
• Each parameter in the parameterList has
procedure scope, which means it can be used only
within the procedure
• The data is passed to the procedure through the
Call statement’s argumentList, which is a commaseparated list of arguments you want passed to the
procedure
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
7
Passing Variables
• Variables have both a value and a unique address that
represents the location in the computer’s internal memory
• You pass either a copy of the variable’s value or its address
to the receiving procedure
• Passing a copy of the variable’s value is referred to as
passing by value
• Passing
its
address
is
referred
to as
Figure 8-3Illustrations of passing by value
passing by reference
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
and passing by reference
8
Passing Variables
(cont'd.)
Passing Variables by Value
• To pass a variable by value, you
include the keyword ByVal
• A copy of the variable’s
contents are passed to the
receiving procedure
• The CheckedChanged event
occurs when the value in the
Checked property changes
9
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Figure 8-4: Actor/Actress application
Passing Variables
(cont'd.)
Passing Variables by
Reference
• To pass a variable by reference,
you include the keyword ByRef
• The receiving procedure has
access to the variable being
passed
10
Figure 8-5: Modified Lanza Trinkets application
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Passing Variables
(cont'd.)
• Desk-checking refers to the process of reviewing the
program instructions while seated at your desk
rather than in front of the computer
• Also called hand-tracing because you use a pencil and
paper to follow each of the instructions by hand
11
Figure 8-5: Pay calculations using sample input values
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Passing Variables
(cont'd.)
Figure 8-6: Pay calculations using sample input values
Figure 8-7: Desk-check table before the Call statement is processed
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
12
Passing Variables
(cont'd.)
Figure 8-8: Desk-check table after the Call statement and CalcOvertime procedure header are
processed
13
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Passing Variables
(cont'd.)
Figure 8-9: Desk-check table after the first statement in the CalcOvertime procedure is processed
14
Figure 8-10: Desk-check table after the True path in the CalcOvertime procedure is processed
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Passing Variables
(cont'd.)
Figure 8-11: Desk-check table after the CalcOvertime procedure ends
15
Figure 8-12: Desk-check table after the gross pay is calculated
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Associating a Procedure with Different
Objects and Events
• Associating a procedure with
more than one object and
event avoids duplicating code
• All of the associated events must have
the same parameters in their procedure
header
• The sender parameter contains a copy of
the object that raised the event
• The e parameter in an event procedure’s
header contains additional information
provided by the object that raised the
event
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Figure 8-13: Click
event procedure for
the exitButton
16
Associating a Procedure with Different
Objects and Events
(cont'd.)
17
Figure 8-14: How to associate a procedure with different objects and events
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Associating a Procedure with Different
Objects and Events
(cont'd.)
18
Figure 8-15: Two versions of some of the code in the Lanza Trinkets application
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Function Procedures
• A Function procedure returns a value after
performing its assigned task
• A Sub procedure does not return a value
• Function procedures are referred to as
functions
• The value is
returned by the
Return statement,
which typically is
the last statement
within a function
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Figure 8-16: Illustration of a Sub procedure and a function
19
Function Procedures
(cont'd.)
20
Figure 8-17: Another example of the difference between a Sub procedure and a function
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Function Procedures
(cont'd.)
21
Figure 8-18: How to create a Function procedure
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Function Procedures
(cont'd.)
Figure 8-19: How to invoke a Function procedure
22
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Function Procedures
(cont'd.)
Figure 8-20: Lanza Trinkets
application using a function
(continues)
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
23
Function Procedures
(cont'd.)
Figure 8-20: Lanza Trinkets application
using a function
24
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Function Procedures
(cont'd.)
Figure 8-21: Desk-check table after the first statement in the False path is processed
25
Figure 8-22: Desk-check table before the Return statement is processed
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Function Procedures
(cont'd.)
Figure 8-23: Desk-check table after the CalcOvertime function ends
26
Figure 8-24: Desk-check table after the gross pay is calculated
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Converting Object Variables
• Unlike variables declared using the String and
numeric data types, variables declared using the
Object data type do not have a set of properties
• This is because there are no common attributes
for all of the different types of data that can be
stored in an Object variable
• Converting a variable from one data type to another
is sometimes referred to as type casting or, more
simply, as casting
• You can cast a variable from the Object data type
to a different data type using the TryCast operator
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
27
Converting Object Variables
(cont'd.)
28
Figure 8-25 How to use the TryCast operator
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
The Full Name Application
Figure 8-26 Two ways of coding each text box’s
Enter event
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
29
Using a Timer Control
• A timer control processes code at one or more
regular intervals
• The length of each interval is specified in
milliseconds (1000 milliseconds in a second ) and
entered in the timer’s Interval property
• The timer’s state—either running or stopped—is
determined by its Enabled property, which can be
set to either True or False
• Each time the Tick event occurs, the computer
processes any code contained in the Tick event
procedure
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
30
Using a Timer Control
(cont'd.)
The Timer Example
Application
Figure 8-27: Timer Example application
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
31
Programming Tutorial 1
Figure 8-34 Interface
showing the monthly
usage and total charge
32
Figure 8-28 TOE chart for the Tri-County Electricity application
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Programming Tutorial 2
Figure 8-47: Game board
showing the first word
assigned to the label
33
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Figure 8-36: TOE chart for the Concentration Game
application
Programming Example
Figure 8-49: TOE Chart
34
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Figure 8-50: MainForm and tab order
Summary
• An event procedure is a Sub procedure that is associated with
one or more objects and events
• Independent Sub procedures and Function procedures are
not associated with any specific object or event
• The difference between a Sub procedure and a Function
procedure is that a Function procedure returns a value,
whereas a Sub procedure does not return a value
• Procedures allow programmers to avoid duplicating code in
different parts of a program
• You can use the Call statement to invoke an independent Sub
procedure. The Call statement allows you to pass arguments
to the Sub procedure
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
35
Summary
(cont'd.)
• When calling a procedure, the number of arguments listed
in the argumentList should agree with the number of
parameters listed in the parameterList in the procedure
header
• You can pass information to a Sub or Function procedure
either by value or by reference
• To pass a variable by value, you precede the variable’s
corresponding parameter with the keyword ByVal
• To pass a variable by reference, you precede the variable’s
corresponding parameter with the keyword ByRef
• The procedure header indicates whether a variable is being
passed by value or by reference
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
36
Summary
(cont'd.)
• When you pass a variable by value, only a copy of
the variable’s contents is passed
• Variables that appear in the parameterList in a
procedure header have procedure scope, which
means they can be used only by the procedure
• You can use an event procedure’s Handles clause
to associate the procedure with more than one
object and event
• You invoke a Function procedure, also called a
function, by including its name and any arguments
in a statement
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
37
Summary
(cont'd.)
• You can use the TryCast operator to convert an
Object variable to a different data type
• The purpose of a timer control is to process code at
one or more specified intervals
• Start a timer by setting its Enabled property to True
• Stop a timer by setting its Enabled property to False
• Use a timer’s Interval property to specify the number of
milliseconds that must elapse before the timer’s Tick event
occurs
38
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Purchase answer to see full
attachment