Main Menu

Recursiveness troubles

Started by Mahan, May 31, 2010, 06:09:18 AM

Previous topic - Next topic

Mahan

Hi guys,

I'm new to PlayBasic  :)

I wanted to ask you guys why the function c() fails in my code below.

I get this error message:

WARNING FATAL RUN TIME ERROR

Fatal Error - Stack POP expecting value.

Error Number: (4)  at Line: (18)

EndFunction Count

Error caused by:



PlayBASIC Code: [Select]
function a(count)
if count = 100 then count = count + 101
EndFunction count

function b(count)
if count = 100 then count = count + a(count+1)
EndFunction count

function c(count)
if count = 100 then count = count + c(count+1)
EndFunction count

print "a() = " + str$(a(100))
sync:WaitNoKey:WaitKey
print "b() = " + str$(b(100))
sync:WaitNoKey:WaitKey
print "c() = " + str$(c(100))
sync:WaitNoKey:WaitKey





kevin

Howdy Mahan,

 Well, firstly while recursion is supported, I wouldn't say it's a strength of the runtime (ie. a lot of overhead).   However,  looking at your example there's certainly an issue with the runtimes control of the stack in this particular situation.  
 
 Can you show what you're actually trying to do ?

Mahan

Quote from: kevin on May 31, 2010, 09:38:03 AM

  Can you show what you're actually trying to do ?


Well, I was just fooling around, reading the docs and checking the language features: Some bits of it are cool like the ability to return multiple values from functions. So I wanted to experiment how this "multi return" worked in conjunction with recursivity, but only got so far as to normal single-value returns before I got this error. And after searching the help and the forums, and not finding anything, I wanted to know that it wasn't me making some classic newbie misstake of some sorts :)

kevin

 
QuoteI wanted to know that it wasn't me making some classic newbie misstake of some sorts

  ;) - While that does happen from time to time, it's the run time in this case.  I suspect it's due to some changes made pretty recently actually.   Anyway, i've made a note of it in the bug forum.  So it'll certainly get looked at !