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 4 Years Ago by RASHIDDDDD: n/a

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 4 Years Ago by Jx_Man: n/a

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 4 Years Ago by BitBlt: n/a

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 article has been dead for over six months. Start a new discussion instead.