Modular programming with your own function 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 function methods. You will learn to write our own subprogram methods in another note.

I created my own function in writing this payroll program that deducts 20% tax. Here is a listing of the event handler for clicking on the Compute Pay button:

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
   Dim hr As Single
   Dim hw As Single
   hr = CSng(txtRate.Text)
   hw = CSng(txtHours.Text)
   txtNet.Text = CStr(NetPay(hr, hw))
End Sub
As you see, it uses a function called NetPay. NetPay is a numeric function that returns a single precision number. It has two numeric arguments, an hourly rate and the number of hours worked, and returns the net pay.

However, NetPay is not built into VB.NET; I defined it myself. Here is the listing of its definition:

Private Function NetPay(ByVal rate As Single, ByVal hours As Single) As Single
   Dim gross, net As Single
   gross = rate * hours
   net = 0.8F * gross
   Return net
End Function
The first line of the function definition gives the name and data type of the function and the data types of its arguments. The syntax of that line is:
Private Function <function name> ([<argument-definition list>]) As <data type>
The keyword Private limits the use of this function to the current form. (If it were Public, its scope would be broadened to any form in a multi-form program).

The <argument-definition list> names and declares the arguments of the function. If there are more than one, they are separated by commas. Some functions do not have arguments, so the <argument-definition list> is optional. (Do not worry about the keyword ByVal at this point -- we will discuss its meaning later). Finally, note that the order of the arguments from left to right is significant. The calling program must supply arguments in the order in which they appear in the definition of the function.

Looking at the body of the function definition we see that it computes the net pay using the rate and hours arguments and local variables called gross and net. It is good practice to declare all the variables you use within the function to avoid accidently changing a global variable of the same name. (Remember that our goal is to divide the program up into completely separate sub-programs).

The last statement before End Function causes the value of the variable Net to be returned to the calling program. The syntax of the Return statement is:

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

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

An interesting twist on functions is that they can be used in their own definitions. A function that calls itself is called a recursive function.

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.