Hi
I have made some experiments with DEF Fn in QBasic and yes I have missed that feature to access to the main module's variables without SHARED in DEF Fn or DIM SHARED in main module. Moreover SHARED is forbidden into DEF Fn multiline.
Surely it is impossible to declare in DEF Fn multiline a variable with the same name of those in main module already declared, but as
good Qbasic/QB bug you can declare a variable in main and also in DEF Fn multiline with DIM if DEF Fn is before the declaration in main module!.
So I think that if it is impossible to distinguish between variables of DEF FN multiline and variables used in DEF fn multiline but declared into main module.
if you run this code into Qbasic
DEF FnDue(z%)
DIM a%
PRINT a%
FnDue = z% * a%
END DEF
CLS
a% = 10
PRINT a%
PRINT FnDue(4)
END
and after F5 you'll get as output
10
10
40
This output lets us think that the code is running first main module sequence and after the DEF Fn sequence.
But is it legal in QBasic/QB/QB64 declare a variable with assignation and then declare the same variable with DIM?
NOSo the first line of DEF FN multiline
must activate the error like we get if in main we type
just if a% in the main module and DIM a% in DEF Fn are the same variable! In the case that the two previous variables are different (and not the same) we must get as output
10
0
0
because in DEF Fn a% has no assignment . Indeed here we found another dual experience of principle of Heisenberg
https://en.wikipedia.org/wiki/Uncertainty_principle :-) we don't know the whole universe!
Coming back to the translator of DEF Fn to FUNCTION we must admit that
the issue of broken correlation between main module and DEF Fn multiline comes out for DEF Fn multiline.The point of gray is the real possibility to define a variable into DEF FN using DIM with the same name of a variable declared into main module using the assignment statement. (as showed into following code)
DEF FnQbasicBug (z%)
DIM a%
FnQbasicBug= z% * a%
END DEF
a% = 10
PRINT a%, FnQbasicBug(4)
and this strange behaviour is claimed in the same help of Qbasic with a warning
don't modify global variable into DEF Fn and don't execute graphic statements into DEF Fn.Well in conclusion I think that the issue can be solved except for the rare case of a coder that declare in DEF Fn a variable with the same name of another variable declared into the main module .
Waiting suggestions to solve this task for now I have thought an array for a list of variable of main to shared.
Thanks to read and talk.