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

Recommended Answers

All 7 Replies

Any error appear?

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

any1 wit answer????

any1 wit answer????

Did you read my post?

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!

So is ByVal the default?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.