Problem w/Function.. PLEASE HELP

jpetter7

Member
Joined
Jul 20, 2004
Messages
16
Programming Experience
Beginner
I am writing a simple code using a function to return a value.
When I run the code, the function always returns one, therefore giving me a logical error. Any and all help is appreciated... Thank you!!
Here is the code:

Private Function Factorial(ByVal intNumber As Integer) As Integer
Dim intFac As Integer = 1
Dim i As Integer
For i = 2 To intNumber
intFac = intFac * 1
Next
Factorial = intFac
End Function

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim intNumber, intFactorial As Integer
Try
intNumber = Convert.ToInt32(txtNumber.Text)
intFactorial = Factorial(intNumber)
Debug.WriteLine(intFactorial, "Factorial = ")
txtFactorial.Text = intFactorial.ToString()
Dim i As Integer
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
 

dynamic sysop

Member
Joined
Jun 27, 2004
Messages
10
Location
Ashby-De-La-Zouch, UK.
Programming Experience
5-10
you are multiplying 1 by 1 ( intFac = intFac * 1 ) is the same as ( 1 = 1 * 1 ) ;)

shouldn't it be intFac = i * 1
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
i'm not seeing the point of the *1 part of the whole thing, because you might as well just add the numbers instead of number * 1 (which equals the same number again)

Number * 1 = Number
so here's what that function is doing (fixing the intFac = inFac line)

VB.NET:
  Private Function Factorial(ByVal intNumber As Integer) As Integer
	Dim I as Integer, intFac As Integer = 1
	For I = 2 To intNumber
	'intFac = inFac *1  'this is the old line of code
	intFac += intNumber  'or intFac += I  but that would make for a stupid function
	Next
	Return intFac
  End Function
 

jpetter7

Member
Joined
Jul 20, 2004
Messages
16
Programming Experience
Beginner
Thanks for your responses.... It does need to be intFac * i

NOT intFac * 1 :eek:)
Lol, yesterday was a long day guys.. thanks for the quick help!
 
Top Bottom