Modular programming with your own subprogram methods

Small programs are easy to understand, but as soon as they get large, they get confusing. To deal with the complexity of large programs, programmers break them into small, self-contained parts which can be written and debugged independently, and re-used in other programs.

If this sounds familiar, it should. The methods we have been using are all small programs with well-defined purposes. They were supplied by MicroSoft as part of VB.NET.

In this note, you will learn to write your own subprogram methods. You will learn to write our own function methods in another note.

This payroll program computes net pay with the tax rate fixed at 20%. It uses a subprogram called NetPay. I wrote NetPay; it was not supplied by Microsoft.

Private Sub btnCalculate_Click . . .
   Dim hr As Single
   Dim hw As Single
   hr = CSng(txtRate.Text)
   hw = CSng(txtHours.Text)
   Call NetPay(hr, hw)
   txtNet.Text = CStr(netresult)
End Sub

As you see, the event handler gets the input from the user, then calls the NetPay subprogram. NetPay calculates the net pay, leaving the result in the global variable NetResult. When the subroutine ends, execution resumes with the statement following the Call, and the value of the global variable NetResult is displayed in the text box called TxtNet.

The NetPay subroutine is shown below:

Private Sub NetPay(ByVal rate As Single, ByVal hours As Single)
   Dim grossPay As Single
   grossPay = rate * hours
   netresult = 0.8F * grossPay 'assign NetResult (a global variable)
End Sub
This subroutine has two arguments, Rate and Hours, and it declares a local variable GrossPay. It calculates the net pay and assigns it to a global variable called NetResult. When it reaches the EndSub statement, control is returned to the statement immediately following the Call to the subroutine.

We have introduced a new statement in this example, the Call statement. The syntax of the Call statement is:

Call <subprogram name> ([<list of arguments>])
As with functions, there might not be any arguments, but if there are arguments, they must be in the correct order and of the correct type.

Step five in our program development process is coding event handlers and writing common code. This includes planning the subprograms you will be writing. Begin by creating an external design of the subprogram -- how many arguments will it have? What is the data type of each argument? What will its effect be?

Once you have answers to these questions, you can write the first line of the subprogram, then code the body. Once the subprogram is coded and tested, you can use it in any program you write.

Finally, see this related note for an example of modular programming, .


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.