Today’s Menu:
Functions…
To outline what you are going to do. Instead of doing it there, you write it somewhere else and give it a call.
There are two types of functions.
· Built in functions – most mathematical functions are built in funtions.
· User defined functions
Referenced
from:
http://72.14.207.104/search?q=cache:oxLTmINI14QJ:www.math.uaa.alaska.edu/~afkjm/cs109/handouts/Chap1.pdf+vb+function+calculate+gross+pay&hl=en&gl=us&ct=clnk&cd=6
Computing
Gross
Pay
Display message: "How many hours did you work?"
Allow user to enter number of hours worked
Store the number the user enters in memory
Display message: "How much are you paid per hour?"
Allow the user to enter an hourly pay rate
Store the number the user enters in memory
Multiply hours worked by pay rate and store the result in
Memory.
Display a message with the result of the previous step
This well-defined, ordered set of steps for solving a problem
is called an algorithm
referenced from:
http://www.vbtutor.net/lesson11.html
11.1 Creating Your Own Functions
The general format of a function is as follows:
Public Function functionName
(Arg As dataType,..........) As dataType
or
Private Function functionName (Arg As dataType,..........) As dataType
* Public indicates that the function is applicable
to the whole program and
Private indicates that the function is only
applicable to a certain module or procedure.
Example 11.1
In this example, a user can calculate future value of a certain amount of money he has today based on the interest rate and the number of years from now supposing he will invest this amount of money somewhere). The calculation is based on the compound interest rate.

Public Function FV(PV As Variant, i As Variant, n As Variant) As Variant
'Formula to calculate Future Value(FV)
'PV denotes Present Value
FV = PV * (1 + i / 100) ^ n
End Function
Private Sub compute_Click()
'This procedure will calculate Future Value
Dim FutureVal As Variant
Dim PresentVal As Variant
Dim interest As Variant
Dim period As Variant
PresentVal = PV.Text
interest = rate.Text
period = years.Text
FutureVal = FV(PresentVal,
interest, period)
MsgBox ("The Future Value is " & FutureVal)
End Sub
Our version with some minor changes
without functions.
Dim FutureVal As
Double
Dim PresentVal As Double
Dim interest As Integer
Dim period As Integer
PresentVal = PV.Text
interest = Rate.Text
period = years.Text
FutureVal = FormatCurrency(PresentVal * (1 + interest / 100) ^ period)
MsgBox("The Future Value is " & FutureVal)

Now, same program using functions…
Function computefuturevalue(ByVal PresentVal, ByVal interest, ByVal period)
computefuturevalue = FormatCurrency(PresentVal * (1 + interest / 100) ^ period)
End Function
Private Sub compute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles compute.Click
Dim FutureVal As Double
Dim PresentVal As Double
Dim interest As Integer
Dim period As Integer
PresentVal = PV.Text
interest = rate.Text
period = years.Text
FutureVal = computefuturevalue(PresentVal, interest, period)
MsgBox("The Future Value is " & FutureVal)

Convert Case Study 5 to Case Study 6: Work with a function
Use a function and make it to work…