Microsoft Visual Basic 2008: Reloaded, Third Edition 8-1

Chapter 8 Sub and Function Procedures

Chapter Overview

Chapter 8 provides an introduction to Sub and Function procedures. Students learn how to create procedures with parameters and pass arguments to procedures. Students also learn the difference between passing data by reference and by value. The technique for associating a procedure with multiple objects and events is presented. Students also learn about the timer control and the TryCast operator.

Chapter Objectives

After studying this chapter, the student should be able to:

•  Explain the difference between a Sub procedure and a Function procedure

•  Create a Sub procedure and a Function procedure

•  Create a procedure that receives information passed to it

•  Explain the difference between passing data by value and passing data by reference

•  Associate a procedure with more than one object and event

•  Explain the purpose of the sender and e parameters

•  Utilize a timer control

•  Convert an object variable to a different type using the TryCast operator

Teaching

Tip / Remind students that they have already been using event procedures, such as the Click event of a button control.

Teaching

Tip / Point out that the Windows programming environment is an event-driven model, and nothing occurs until an event occurs.

1.  Discuss the advantages of using independent Sub procedures.

2.  Describe the syntax for creating an independent Sub procedure using Figure 8-1.

Teaching

Tip / Verify that students understand that a procedure’s code is not processed unless the procedure is invoked from somewhere else in the program.

3.  Introduce the concept of a parameter.

Teaching

Tip / Students often have difficulty understanding the nature of parameters. Explain that parameters are actually specialized local variables that are declared in the procedure header, allowing data to be passed into the procedure.

4.  Introduce the Call statement and use Figure 8-2 to describe how to invoke an independent Sub procedure.

Teaching

Tip / It is not necessary to use the Call statement to invoke a procedure. The procedure name alone as a statement will also work.

Including Parameters in an Independent Sub Procedure

1.  Review the concept of a parameter.

2.  Stress the fact that when passing parameters, the data passed into a procedure must match the declared parameters in number, type, and order.

3.  Describe the types of values that can be passed to an independent Sub procedure.

Passing Variables

1.  Describe the two ways that variables can be passed to procedures.

Teaching

Tip / It may be helpful to describe passing by value as equivalent to “read-only” access to the variable being passed in, while passing by reference might be described as having “read-write” access.

Passing Variables by Value

1.  Introduce the ByVal keyword for passing a parameter by value.

Teaching

Tip / Verify that students understand that passing a variable by value does not allow the procedure to change the actual variable’s value.

2.  Using Figures 8-5 and 8-6, discuss the sample application code, with an emphasis on the declaration of the Sub procedure and on the Call statement that invokes it.

Passing Variables by Reference

1.  Introduce the ByRef keyword for passing a variable by reference.

2.  Describe how passing a variable by reference results in passing the variable’s memory address.

Teaching

Tip / Be sure to point out that only one of the parameters in this sample code is passed by reference.

3.  Using the tables in Figures 8-9 and 8-10, describe how the variables in memory change after processing the sample code.

Associating a Procedure with Different Objects and Events

1.  Review the concept of the Handles clause and remind students that this is how objects and their events are linked to event procedures.

2.  Point out that the default event procedure name for a control’s event includes both the object name and the event name as shown in Figure 8-11.

3.  Point out that the name of the event procedure can be changed, and it can be associated with more than object and event.

Teaching

Tip / It is a good idea to rename the event procedure name when associating it with more than one object. It is easy to forget to look at the Handles clause because it is often scrolled out of view in the IDE. Using a procedure name that is more generic will help to remind you that this event procedure is not limited to a single control.

4.  Review the sample application code shown in Figure 8-12, emphasizing the multiple objects and their events listed in the Handles clause.

Teaching

Tip / Associating a single event procedure with multiple objects and their events can eliminate a great deal of code duplication in cases where the same processing must be done to multiple controls.

5.  Introduce the Sender and e parameters found in event procedures.

Function Procedures

1.  Introduce the concepts of a function and a Function procedure.

2.  Point out that Visual Basic provides many built-in functions.

3.  Introduce the Return keyword for allowing the function to pass a value back to the code that invoked it.

Teaching

Tip / Point out that the Return keyword not only causes the value to be returned to the code that invoked the function, but it also causes the flow of control to return to the point where the function was invoked, thus effectively ending the function.

Teaching

Tip / Emphasize the need for the return type as part of the function declaration.

4.  Compare and contrast the Function procedure declaration with that of the Sub procedure.

The Timer Control

1.  Introduce the timer control and describe its purpose.

2.  Use Figure 8-16 to explain that the timer control does not appear on the form at design time or at run time.

3.  Explain the use of the timer’s Tick event and Interval property using Figure 8-17.

Class Discussion Topics

1.  The use of functions and procedures allows a developer to employ the divide and conquer technique to the development of a large program. What are the advantages of using this approach?

  1. At first glance, it may seem that most of the processing to be done in event procedures and/or in independent Sub procedures and functions are very specific to the particular program under development. What can be done to create procedures and functions that are more generalized and can be reused in other programs?

Additional Projects

  1. Write the pseudocode for a function that calculates a student’s test average, given three test scores, and a percentage curve to be applied to the final average. All values required for the calculation should be passed in as parameters.

Additional Resources

  1. Procedures and functions:

www.startvbdotnet.com/language/methods.aspx

  1. Using functions and arguments:

www.codeguru.com/vb/gen/vb_misc/samples/article.php/c8943/

  1. Handling events:

http://visualbasic.about.com/od/learnvbnet/a/eventhandler.htm

  1. Using the Timer control:

www.vbdotnetheaven.com/Uploadfile/mahesh/TimerControl04262005033148AM/TimerControl.aspx

  1. Functions vs. Subroutines and By Val vs. by Ref in VB.NET:

www.codeproject.com/KB/aspnet/VBnet_Methods.aspx

Key Terms

Ø  Call statement—the Visual Basic statement used to invoke an independent Sub procedure in a program

Ø  Function—another term for a Function procedure

Ø  Function procedure—a procedure that returns a value after performing its assigned task

Ø  Independent Sub procedure—a procedure that is not associated with any specific object or event and is processed only when invoked (called) from code

Ø  Parameters—the memory locations listed in a procedure header

Ø  Passing by reference—the process of passing a variable’s address to a procedure

Ø  Passing by value—the process of passing a variable’s contents to a procedure

Ø  Procedure—a block of program code that performs a specific task

Ø  Return statement—the Visual Basic statement that returns a function’s value to the statement that invoked the function

Ø  Sub procedure—a procedure that does not return a value after performing its assigned task

Ø  Timer control—the control used to process code at one or more regular intervals

Ø  TryCast operator—used to convert an Object variable to a different data type