The idea of breaking a large application into smaller, more manageable sections is not new to computing. Few tasks, programming or otherwise, can be managed as a whole. The event handlers are just one example of breaking a large application into smaller tasks.
For example, when you write code for a control’s Click event, you concentrate on the event at hand — namely, how the program should react to the Click event. What happens when the control is double-clicked or when another control is clicked is something you will worry about later — in another control’s event handler. This divide-and-conquer approach isn’t unique to programming events. It permeates the Visual Basic language, and even the longest applications are written by breaking them into small, well-defined, easily managed tasks. Each task is performed by a separate procedure that is written and tested separately from the others. As mentioned earlier, the two types of procedures supported by Visual Basic are subroutines and functions.
Subroutines usually perform actions and they don’t return any result. Functions, on the other hand, perform some calculations and return a value. This is the only difference between subroutines and functions. Both subroutines and functions can accept arguments, which are values you pass to the procedure when you call it. Usually, the arguments are the values on which the procedure’s code acts. Arguments and the related keywords are discussed in detail in the “VB.NET Arguments” section later in this chapter.
A subroutine is a block of statements that carries out a well-defined task. The block of statements is placed within a set of Sub. . .End Sub statements and can be invoked by name.
The following subroutine displays the current date in a message box and can be called by its name, ShowDate():
Sub ShowDate() MsgBox(Now().ToShortDateString) End SubCode language: VB.NET (vbnet)
Normally, the task performed by a subroutine is more complicated than this; but even this simple subroutine is a block of code isolated from the rest of the application. The statements in a subroutine are executed, and when the End Sub statement is reached, control returns to the calling program. It’s possible to exit a subroutine prematurely by using the Exit Sub statement.
All variables declared within a subroutine are local to that subroutine. When the subroutine exits, all variables declared in it cease to exist.
Most procedures also accept and act upon arguments. The ShowDate() subroutine displays the current date in a message box. If you want to display any other date, you have to implement it differently and add an argument to the subroutine:
Sub ShowDate(ByVal birthDate As Date) MsgBox(birthDate.ToShortDateString) End SubCode language: VB.NET (vbnet)
birthDate is a variable that holds the date to be displayed; its type is Date. The ByVal keyword means that the subroutine sees a copy of the variable, not the variable itself. What this means practically is that the subroutine can’t change the value of the variable passed by the calling application. To display the current date in a message box, you must call the ShowDate() subroutine as follows from within your program:
ShowDate()Code language: VB.NET (vbnet)
To display any other date with the second implementation of the subroutine, use a statement like the following:
Dim myBirthDate = ShowDate(myBirthDate)Code language: VB.NET (vbnet)
Or, you can pass the value to be displayed directly without the use of an intermediate variable:
ShowDate(Code language: VB.NET (vbnet)
If you later decide to change the format of the date, there’s only one place in your code you must edit: the statement that displays the date from within the ShowDate() subroutine.