VB Functions II


© Swapna Kamat
Articles in this Topic    Discussions in this Topic

VB Functions II

Last time, we studied some important functions in Visual Basic. Lets take a look today at creating user-defined functions.

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.

Let us take an 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

We try to implement a program which computes grades of a student based on the mark he scores.

Public Function grade(mark As Variant) As String
Select Case mark
Case Is >= 80
grade = "A"
Case Is >= 70
grade = "B"
Case Is >= 60
grade = "C"
Case Is >= 50
grade = "D"
Case Is >= 40
grade = "E"
Case Else
grade = "F"
End Select

End Function

Private Sub compute_Click()

grading.Caption = grade(mark)

End Sub

Private Sub End_Click()
End

End Sub

Go To Page: 1


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo