Calling the Subroutine

Firefighters put out fires. However, they generally do not drive around looking for fires. Instead, they go out to a fire when called upon to do so.

In the same way, a subroutine does not just execute by itself. The statements within a subroutine do not execute until and unless the subroutine is called.

A subroutine may be called by user action or by code. Event procedures usually are called by user action. In the preceding example, in which the caption of a form was changed to "Clicked", the code inside the event procedure executed because of the user action of clicking a form.

By contrast, you could call the PrintInput subroutine from codeā€”for example, from an event procedure such as the Click event of the form:

Private Sub Form1_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles MyBase.Click


End Sub

Note According to MSDN, if there are no arguments, the parentheses are optional. However, you may find that the Visual Basic .NET IDE insists on putting the parentheses after the name of the called subroutine.

Optionally, you can use the Call statement to call the PrintInput subroutine: Call PrintInput()

Tip The Call statement is a holdover from prior versions of Visual Basic.

There really is no need to use it with subroutines.

Continuing the firefighter analogy, when firefighters arrive at the scene of the fire, they take control and maintain that control until they put out the fire. Similarly, once the subroutine is called, whether by user action or code, it takes control of the application, and no other code executes without being called by the subroutine, until the subroutine is finished.

Completing the analogy, when the firefighters successfully put out the fire, they pack up their equipment and go back to the fire station, relinquishing control of the fire scene. Similarly, when the subroutine finishes executing, it relinquishes control of the application, and whatever code (or user action) follows the call of the subroutine determines the further flow of the application.

Was this article helpful?

0 0

Post a comment