QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: GTC on December 18, 2020, 08:14:33 am
-
QB64 compiler (v1.4) is warning me about an unused string variable which in fact is used.
Here's a summary of the situation:
DIM SHARED F$
Call Blah ("hello")
End
Sub Blah (F$)
Print F$
End sub
-
If you're passing it as a parameter, you don't need it to be shared. If you're sharing it, you don't need to pass it as a parameter.
QB64 is having a hard time understanding your duality of variable naming - and I'm afraid I'll have to side with it ☺️
-
- and I'm afraid I'll have to side with it ☺️
Aha, and you' be correct! :-)
Thank you.
-
This will shed some more light:
[ This attachment cannot be displayed inline in 'Print Page' view ]
The local parameter takes precedence over a shared variable. Behaviour confirmed in QB4.5 too.
-
If you look at the c-translated code we output in internal/temp/main.txt, you'll see that the two are completely different beasts.
DIM SHARED F$ --- In the main module, this will translate down to STRING_F, or something similar.
Sub Blah (F$) --- And this will translate down to STRING_BLAH_F, or something similar, which is completely different, and localized to Blah only.
Conclusion: You can't SHARE the same variable as you're passing as a parameter.
On the same note, you can't use STATIC inside a SUB for the same reason.
DIM SHARED Foo
Foo = 1
PRINT Foo
Example
PRINT Foo
SUB Example
STATIC Foo
Foo = 2
PRINT Foo
END SUB
Passing via parameter, or use of STATIC, makes the variable unique to the sub and not the same as one you might be sharing elsewhere.
-
https://en.m.wikipedia.org/wiki/Variable_shadowing