Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VB recursion error
#1
Code:
Function factorial(n As Double) As Double
If n < 2 Then
  factorial = 1
Else
  factorial = n * factorial(n - 1)
End if
End Function
That's my recursive factorial-figurer. But when I try to use it to calculate even the smallest factorials, like 3! or 4!, I get the runtime error "Out of stack space".
Is it just my computer?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#2
Two things:

1.- Maybe you need to set up an auxiliary variable to store the results, and then assign it to factorial (I'm not sure, but that's how I do it). Maybe that "factorial = 1" in the base case is doing nasty things (senseless calls and stuff).

2.- Upon the definition of factorial it only works with integer numbers, so it would be less correct to use LONGs (you can use Doubles if you are careful enough not to send decimal numbers to the function).

Code:
Function factorial(n As Long) As Long
   Dim res As Long
   If n < 2 Then
      res = 1
   Else
      res = n * factorial(n - 1)
   End if
   factorial = res
End Function
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#3
VB doesn't have a very large stack... :-?

Anyway, why do you need a recursive function to calculate the factorials?

Can't you just do this:
Code:
Private Function Fac (n As Integer) As Long
   If n <= 1 Then Fac = 1: Exit Function
   Dim lngRes As Long, intT As Integer
   lngRes = 1
   For intT = 2 To n
      lngRes = lngRes * intT
   Next intT
   Fac = lngRes
End Function
Works faster and cleaner and doesn't need as much stack space as the recursive function Smile
Reply
#4
Neo: True, true, but I wanted for practice in computer sci to write this thing.
In QB, isn't there a command to allocate more stack space? Does that apply to QB as well?
[EDIT]Nath, thanks, doing that worked. [/EDIT]
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#5
I dunno whats wrong with our CP teacher as well. They insist on using recursion as well :???:
Reply
#6
Recursion is very useful at times...for instance, walking a directory structure or writing a threaded forum.
Reply
#7
In those cases Yes. But not in case of factorials and such stuff :wink:
Reply
#8
Well, hey...it was in the vb textbook I'm studying, and I've encountered it before (recursion) and I never really understood it. So I thought, why not implement it in a *Very* easy language?
(I've done it in assembly before, a nightmare).
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#9
Quote:In those cases Yes. But not in case of factorials and such stuff :wink:

Well, that's only for educational purposes. Factorial is itself a really simple function to illustrate the base case and the recursive case, it is short, and can be understood easily.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)