Recursion

BlitzPlus Forums/BlitzPlus Programming/Recursion

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Your functions works, it just looks funny.
Every time you call test again, the new value gets pushed onto the stack:
1,2,3,45,6,7,8,9,10

then when print finally get control of it it pops the values off the stack, and presto"
10,9,8,...
The last number your function pushed on the stack is 10 and it gets popped first, of course, that why you see a reverse order of the numbers you expected. This probably wouldn't be an issue if you didn't call test again with the value of the parameter, all dished to the print function, which can't print a value yet, because the function has to execute to return the value to be printed. And so on...

When everything is done, print finally gets the values to be printed (popped from stack-> last being first etc) and executes, that why you see the "unexpected" reversing.

If you change the code to this:

Function test#(parm)
test#=parm+1
If test<10 Then
Print test
test(test)
endif
Return test
End Function

you will probably get what you expected. This is NOT a BB bug, its a logic error on the programs behalf...

Hope that helps...