函数的一般格式如下:
Public Function functionName (Arg As dataType,..........) As dataType
�
Private Function functionName (Arg As dataType,..........) As dataType
* Public表明该函数是适用于整个程式,
Private 则表明该函数只适用于某个模块或子程序�
程序代码
Public Function FV(PV As Variant, i As Variant, n As Variant) As Variant
End Function
Private Sub compute_Click()
Dim FutureVal As Variant
Dim PresentVal As Variant
Dim interest As Variant
Dim period As Variant
PresentVal = TxtPV.Text
interest = TxtRate.Text
period = TxtYears.Text
FutureVal = FV(PresentVal, interest, period)
LblFV.Caption=FutureVal
End Sub
以下函数将自动计算学生获得的考试分数和等级,输出屏幕如图14.2 所示�
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()
Lbl.Caption = grade(txtMark)
End Sub