#### jpetter7

##### Member
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
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
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
Thanks for your responses.... It does need to be intFac * i

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

Replies
4
Views
650
Replies
2
Views
296
Replies
1
Views
319
Replies
1
Views
162
Replies
0
Views
188