[求助]function函数用法
有谁能告书我function函数的用法!最好再附上简单的例子的源代码!
俺也是初学。从教材学的,共享吧。
函数是过程的另一种形式,它的执行结果返回一个值,所以,可以用在等号的右边,如:a=add(5)
语法格式为:
[Publice|Private|Friend][Static]Function name[(arglist)][as type]
[statements]
[name=expression]
[exit function]
[statements]
[name=expression]
end function
例:返回n个自然数的和
Option Explicit
Function addn(n As Integer) As Long
Dim i As Integer
Dim total As Long
total = 0
For i = 1 To n
total = total + i
Next i
addn = total '给函数赋值
End Function
Private Sub cmdadd_Click()
Dim A As Integer
Dim B As Long
A = txtnum.Text
B = addn(A) '调用函数并使用返回的函数值
txtshow.Text = B
End Sub
Private Sub cmdcls_Click()
txtnum.Text = ""
txtshow.Text = ""
End Sub
Private Sub cmdend_Click()
End
End Sub