Here's an interesting bug I've found messing with my old QuickBASIC programs.
I suspect that anyone has spotted it before, but Google doesn't seem to know anything about this
Please read carefully the following code and guess what it will print:
DIM SHARED a(2)
a(1) = 10
PRINT "before the call:"; a(1)
f a(1)
PRINT "after the call:"; a(1)
SUB f (v)
PRINT "in the sub before modifying:"; a(1)
v = 2
PRINT "in the sub after modifying:"; a(1)
END SUB
Then run it in any ORIGINAL DOS QuickBASIC (QBasic/QB4.5/PDS/VBDOS) and be shocked with its "correct" answer (10 10 10 2 )
The key point here is passing an array element by reference.
Btw this code works as expected in QB64.
And now I wonder how can it be that I've never suffered from this (didn't even know of) in my childhood?
Interesting! So it basically treats the "v" parameter as a local variable in the SUB and doesn't actually update the value of the array element passed until the code is returning from the SUB.
Passing in the whole array works as expected. The source array is updated when the parameter array gets updated.
Ha! I had this issue as well, creating a Frogger clone, and fixed it with passing BY instead of referencing. Just trying to save memory by not using a stupid amount of globals, sheesh!
RETROQB45 wrote: ↑Thu Jun 04, 2020 6:34 am
Here's an interesting bug I've found messing with my old QuickBASIC programs.
.....
Then run it in any ORIGINAL DOS QuickBASIC (QBasic/QB4.5/PDS/VBDOS) and be shocked with its "correct" answer (10 10 10 2 )
Interesting thing: I tried it in QB45, and indeed it happens like you described. Then I tried compiling it (with "make exe file"), and the compiled program returns 10 10 2 2). So, the interpreter is not consistent with the compiler.
Indeed, the "correct" behavior is the most logical one for a compiler (since it works by reference). Looks like the interpreter, on the other hand, works on a copy and tries to adjust the references in the end.