ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Function failure (https://www.excelbanter.com/excel-programming/372437-re-function-failure.html)

davidmy

Function failure
 

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




[email protected]

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




All times are GMT +1. The time now is 03:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com