编写函数fun,函数的功能是:求1到m之间的偶数之积。 Function fun(m As Integer) As Integer Dim k As Integer k =1 For i = 2 To m Step 2 k= k * i Next fun= k End Function Private Sub Command1_Click() Print fun(11) End Sub
sum 函数的功能为求阶乘和, 要求在窗体上显示 nsum = 1 nsum = 3 nsum = 6 nsum = 10 nsum = 15 Private Sub Command1_Click() Dim n As Integer, isum As Integer For n = 1 To 5 nsum = tim(n) Form1.Print "nsum="; nsum Next n End Sub Private Function tim(n As Integer) Static j As Integer j = j + n tim = j End Function
计算数组中各元素的乘积。 Function mul(m() As Integer) Dim t As Double, i% t = 1 For i = LBound(m) To UBound(m) t = t * m(i) Next i mul = t End Function Private Sub Command1_Click() Dim a(1 To 8) As Integer, b%(2 To 12) For i = 1 To 8 a(i) = i Next i For i = 2 To 12 b(i) = i Next i x = mul(a()) y = mul(b()) Print "x="; x, "y="; y End Sub
计算x!和x*y Private Sub Command1_Click() Dim x As Integer, y As Integer x = 3 y = 5 Call f(x, y) 'f x, y End Sub Private Sub f(m As Integer, n As Integer) Dim t, i As Integer t = 1 For i = 1 To m t = t * i Next i q = m * n Print t, q End Sub