怎么在VB语言中给函数过程传递参数?

2025-03-04 23:00:39
推荐回答(2个)
回答1:

Private Sub Form_Click()
Dim s As Integer
Dim x As Integer
Dim y As Integer
s = 5
x = 2
y = 3
a = myfunc(s, x, y)
Print "第" & 5 & "项是:" & a
End Sub
Function myfunc(ByVal s As Integer, ByVal x As Integer, ByVal y As Integer)
If s = 1 Then
myfunc = x
ElseIf s = 2 Then
myfunc = y
Else
myfunc = myfunc(s - 2, x, y) + myfunc(s - 1, x, y)
End If
End Function
上述的代码在遍历中,其中有五次是符合计算要求的第一次的值是:2第二次的值是:3第三次的值是:3第四次的值是:2第五次的值是:3 即2+3+3+2+3=13

回答2:

Option ExplicitPrivate x, y As Integer 'x为第一项值,y为第二项值Private Sub Form_Click()
Dim s, a As Integer

s = 5
x = 2
y = 3

a = myfunc(s)
Print "第" & s & "项是" & a
End Sub
Function myfunc(ByVal s As Integer)
If s = 1 Then
myfunc = x
ElseIf s = 2 Then
myfunc = y
Else
myfunc = myfunc(s - 2) + myfunc(s - 1) '递归调用
End If
End Function