
public或private或dim sub或function 函数名(形参表) as 返回值类型
函数内容
end sub或function
调用时,不需要返回值的时候可以call 函数名加实参表,使用括号,比如:call hanshu(a,b,c)
或者函数名后直接写实参表,不用括号,比如: hanshu a,b,c
有返回值时必须有等号赋值,使用括号,比如: result=hanshu(a,b,c)
这在看你打算让Function XXX返回什么类型的值了,可以是Integer,可以是Long,可以是String也可以是用户自定义类型,等等
举个例子:
public function XXX(byval sTime as String)as String
select case cint( left(format(sTime ,"HH:MM:SS"),2))
case is <12
XXX="上午“
case else
XXX="下午"
end select
end function
private sub Command1_Click
msgbox XXX(Now)
end sub
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()
Text1Text = j(8) '这里就可以调用函数了
End Sub
function是自定义函数,sub是自定义过程,
与数学里一样,函数是有返回一个值的,比如 f(x)=3x+3,当x=2时,f(2)=9,
不过函数与过程没有本质的区别,过程也会返回值的
建议:
如果只是要返回一个值,而没有其他的 *** 作,用函数;如果要返回几个值,或者还有其他的 *** 作,用过程
function 定义的函数的引用与vb自带的函数的引用方法是一样的
例子:
主程序:
Private Sub Command1_Click()
Print fan(2)
End Sub
'定义函数
Function fan(x As Single) As Double
fan = 3 x + 3
End Function
点击按钮后,窗体上显示 9
Private Sub Command1_Click()
Dim a As Integer, b As Integer, s As Integer
s = Max(a, b)
MsgBox ("较大的一个数是:" & s)
End Sub
'自定义函数
Private Sub Max(a As Integer, b As Integer)
If a >= b Then
Max = a
Else
Max = b
End If
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)