Recursive factorial

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hey guys,

I'm trying to learn how to use factorials, this is a simple example:

Code:
Option Explicit
Function factorials(nr As Long, Optional factorialsTest As Long = 1)
Dim test As Variant
factorialsTest = nr * factorialsTest
nr = nr - 1
If nr <> 0 Then Call factorials(nr, factorialsTest)
test = test + 1
End Function
Sub main()
Dim test As Variant
test = factorials(10)
End Sub

Once i run it i get the desired results, but once stepping through the code i noticed that once the if is done(10 cycles), it loops ending of the function 10 times. And even though it keeps going through the test = test + 1, the value keep refreshing to 0, why is this happening? Is there a way to end all the looped functions at once?
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
No need to use a recursive function that calls itself (I would probably recommend against that). Just use a loop like this:
Code:
Option Explicit


Function factorials(nr As Long)
    
    Dim x As Long
    Dim s As Long

    If nr > 0 Then
        s = 1
        For x = nr To 1 Step -1
            s = s * x
        Next x
    End If
    factorials = s

End Function


Sub main()
    Dim test As Variant
    test = factorials(10)
    MsgBox test
End Sub
 
Upvote 0
No need to use a recursive function that calls itself (I would probably recommend against that). Just use a loop like this:
Code:
Option Explicit


Function factorials(nr As Long)
    
    Dim x As Long
    Dim s As Long

    If nr > 0 Then
        s = 1
        For x = nr To 1 Step -1
            s = s * x
        Next x
    End If
    factorials = s

End Function


Sub main()
    Dim test As Variant
    test = factorials(10)
    MsgBox test
End Sub

Hello Joe4,

thank you for your answer, but the whole point of this is to make a recursive function and figure out how they work :)}

Just noticed my initial statement:
I'm trying to learn how to use factorials
was supposed to be:
I'm trying to learn how to use recursive functions...
 
Last edited:
Upvote 0
Upvote 0
Firstly, I am not a recursive function guru. :)

The reason that test keeps going back to 0 is that by the end you actually have 10 functions running. Each of those function has a Dim test as Variant in it. Since you later perform an addition with this variable its initial value is zero in each of the 10 functions. So as you loop back each of those 10 functions vba retrieves the value of test in that function and of course it is zero each time.

If you wanted test to increment, you could do something like this.

Code:
Private test As Long

Function factorials(nr As Long, Optional factorialsTest As Long = 1)
'  Dim test As Variant
  
  factorialsTest = nr * factorialsTest
  nr = nr - 1
  If nr <> 0 Then Call factorials(nr, factorialsTest)
  test = test + 1

End Function

As for exiting all the functions at once, I don't believe so.
 
Last edited:
Upvote 0
The key to recursive functions is that there be one branch that doesn't call the function

Code:
Function MyFactorial (ByVal nr As Long)
    If 1 < nr Then
        MyFactorial = nr * MyFactorial(nr-1)
    Else
        MyFactorial = 1
    End If
End Function

One way to set this up is to start with a non-recursive function that uses a Do loop.
Code:
Function MyOtherFactoria(nr As Long)
    MyOtherFactorial = 1
    Do While 0 < nr
        MyOtherFactorial = MyOtherFactorial * nr
        nr = nr - 1
    Loop
End Function

As to why test isn't incirmenting in the OP. It is scoped at the proceedure level so each interation through the function is a different variable (even though they have the same name)

Changing the declaration line to
Code:
Static test as Long
will change that.
(But you have to re-zero the static variable at the start of your recursive loop)
 
Last edited:
Upvote 0
I also note that your function isn't actually returning a value. That is, the 'test' variable in your 'main' sub remains empty.
It would need something like this added at the end if you were going to use the function somewhere. For example

Rich (BB code):
Private test As Long

Function factorials(nr As Long, Optional factorialsTest As Long = 1)
'  Dim test As Variant
  
  factorialsTest = nr * factorialsTest
  nr = nr - 1
  If nr <> 0 Then Call factorials(nr, factorialsTest)
  test = test + 1
  factorials = factorialsTest
End Function

Sub main()
  Dim Num As Long
  Num = 10
  MsgBox "Factorial " & Num & " = " & factorials(Num)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top