用VB怎么定义一个函数
来源:百度知道 编辑:UC知道 时间:2024/07/07 14:42:57
比如:用VB编好了n的阶乘,但是我想多次使用;定义为f(n);f(3)就等于3的阶乘、f(6)就等于6的阶乘;这样用起来方便些,要怎样编?
Dim t As Long
_________________________________________
Public Function j(n As Integer) As Long
t = 1
For i = 1 To n
t = t * i
Next i
j = t
End Function
_______________________________________
Private Sub Command1_Click()
Text1.Text = j(8) '这里就可以调用函数了
End Sub
实例:
Private Sub xs() ‘自定义了一个过程 xs ()
dim a
dim b
a= "早上好"
b="先生"
Print a
print b
End Sub
Private Sub Form_Click()
call xs() ' 调用自己定义的过程
End Sub
Private Function f(Dim n As Integer) as Integer
if n=0 then
f=1
else
for i=1 to n
t=t*i
next
f=t
endif
End Function
调用的时候如下,x等于24
x=f(4)
Public Function fjiecheng(ByVal n As Integer)
Dim i, fn As Long
fn = 1
For i = 1 To n
fn = i * fn