在vb中,分别用过程和函数编写求阶乘的子程序的程序代码怎么写?

2025-01-06 08:06:22
推荐回答(2个)
回答1:

函数的
Function Jc(x As Integer)
s = 1
For i = 1 To x
s = s * i
Next i
Jc = s
End Function

Private Sub Command1_Click()
Text2.Text = Jc(Val(Text1.Text))
End Sub

过程的
Sub Jc()
s = 1
For i = 1 To Val(Text1.Text)
s = s * i
Next i
Text2.Text = s
End Sub

Private Sub Command1_Click()
Call Jc
End Sub

回答2:

Private Function fact(ByVal x As Integer) As Long
Dim i As Integer
Dim r As Longr = 1
For i = 1 To x
r = r * i
Next
fact = r
End Function