Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 6: Input, Output, and Procedures
Last change: February 13 2016 18:48:15.
<<PreviousNext>>

6.2 Procedures: Subs and Functions

Consider the case of student registration application. The user first enters the student’s ID number as well as all the courses and section numbers that the student intends to enroll. When the user clicks the Process button, the program will proceed to process the request by going through the following steps:

The list can go even longer, but the point is simple: In this situation, if you place all the code in the button’s Click event procedure, it will be hard to follow the program flow of logic. The program will be too long and difficult to read, understand, debug, change, or maintain. The solution to this problem is to divide the program into several subprograms, each handling a predefined special task, such as one of the major steps outlined. The button’s Click event procedure will then provide just an outline of the steps by calling these subprograms. This arrangement will dramatically reduce the number of lines in the event procedure. Because each subprogram handles a smaller task, it is easier to develop code and to understand.
A partial, conceptual solution to the previous problem would probably be similar to the following:

Public Class Enrollment Private Sub btnProcess_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click If BadStudentID(IDNumber) Then MsgBox(“Needs a valid student ID number to proceed.”) Exit Sub End If FindUnPaidRecords(IDNumber) . . End Sub Private Function BadStudentID(ByValue IDNumber As Integer) As Boolean ‘Routine to determine whether the student ID is valid . . End Function Private Sub FindUnPaidRecords(ByVal IDNumber As Integer) ‘Routine to locate student’s unpaid records . . End Sub ‘Other event or general procedures . . End Class

Each procedure referenced inside the event procedure can be written separately as a Sub or as a Function procedure, and can be placed anywhere in the class in the same area as the event procedures. Both types of procedures are different from the event procedure in that the event procedure is typically invoked by the occurrence of an event, whereas the former are invoked by a reference of their names in the code. These procedures are recognized as general (or separate) procedures, as a distinction from the event procedure. The key difference between Sub and Function procedures is that the latter returns a value, whereas the former does not. Additional distinctions are explained in the subsection “Additional Notes on General Procedures,” later in this chapter. For now, the classification of procedures can be summarized as follows.

Procedure Type How Invoked Returns a Value? Remark
Event Triggered by event No  
Sub Referenced by name No Collectively recognized as general or separate procedures
Function Referenced by name Yes
Last change: February 13 2016 18:48:16.
  1. The MsgBox Function
  2. Files
  3. File Dialog Boxes
  • 6.2 Procedures: Subs and Functions
    1. Writing a Sub Procedure
    2. Calling a Sub Procedure
    3. Passing Data to Sub Procedures
    4. Terminating a Procedure Before Reaching the End: Exit Sub or Return
    5. Event Procedures and General Sub Procedures
    6. Function Procedures
    7. Using the Function Procedure
    8. Additional Notes on General Procedures
    9. Uses of General Procedures
    10. <<PreviousNext>>