0

hooow thiiis programm can be done by the method of SUB-PROCEDURE????

Public Function fact(a As Integer)
Dim i As Integer
Dim f As Integer

    f = 1
    For i = 1 To a
        f = f * i
    Next
    
    fact = f
End Function


Private Sub Command2_Click()
Dim a As Integer
a = InputBox("put values")
Print "factorial ="; fact(a)
End Sub

Edited by RASHIDDDDD: n/a

4
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by BitBlt
0

You changed your question...
See if this helps :

Public Function fact(a As Integer)
Dim i As Integer
Dim f As Integer
f = a
For i = a - 1 To 2 Step -1
f = f * i
Next
fact = f
End Function
'------------------------------------

Private Sub Command2_Click()
Dim a As Integer
a = InputBox("put values")
Print "factorial =" & fact(a)
End Sub

Edited by Jx_Man: n/a

0

Try this:

Private Sub Command2_Click()
Dim a As Integer
Dim f As Integer                ' <--- note new variable declaration

a = InputBox("put values")
f = 1                           ' <---- note the variable initialization
factSub a, f                    ' <---- here is the subroutine call

Print "factorial =" + CStr(f)   ' <---- note the conversion function
End Sub

Public Sub factSub(x As Integer, ByRef y As Integer)
Dim i As Integer
For i = 1 To x
    y = y * i
Next
End Sub

Notice that the "y" parameter for the subroutine is declared with the "ByRef" keyword. You use this when you want to change the value of the parameter in the subroutine and have it reflect back in the original subroutine call.

Hope this is what you were looking for. Good luck!

Edited by BitBlt: n/a

0

Hmph...I thought it was, but a short science experiment proved me wrong. It appears "ByRef" is the default...so I guess it was not necessary in my example above.
:icon_redface:
I learned something today.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.