View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
[email protected] paul.robinson@it-tallaght.ie is offline
external usenet poster
 
Posts: 789
Default Function failure

Hi
You havn't specified the value of Fat when n = 1, so the code assumes
it is 0.
Try
Sub Test()
x = Fat(10)
MsgBox x
End Sub


Function Fat(n As Integer) As Long
If n < 1 Then Exit Function
If n = 1 Then
Fat = 1
Else
Fat = n * Fat(n - 1)
End If
End Function

You need the output value as long not integer or you will get an
overflow error.
regards
Paul

davidmy wrote:
I am relaunching my earlier request which was lost in traffic and this
time hoping it will catch the eye of would-be benefactors.
Many thanks.


Myles
davidmy wrote:
[b]I am looking for a way to recursively mimic the FACTORIAL
function. The lesson thus learnt will lead me on to revamp a routine
I am currently working on. Here is my botched attempt.

Sub Test()
x = Fat(10)
Msgbox x
End Sub

Function Fat(n as Integer)
If n < 1 Then Exit Function
Fat = n * Fat(n-1)
End Function

Code returns 0 for any value of n.

TIA
Myles




--
davidmy
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message2500073.html