QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: GTC on December 18, 2020, 08:14:33 am

Title: Unused variable warning, when variable is used as a parameter
Post 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

Title: Re: Unused variable warning, when variable is used as a parameter
Post by: FellippeHeitor on December 18, 2020, 08:17:45 am
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 ☺️
Title: Re: Unused variable warning, when variable is used as a parameter
Post by: GTC on December 18, 2020, 08:22:57 am
- and I'm afraid I'll have to side with it ☺️

Aha, and you' be correct! :-)

Thank you.
Title: Re: Unused variable warning, when variable is used as a parameter
Post by: FellippeHeitor on December 18, 2020, 10:00:43 am
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.
Title: Re: Unused variable warning, when variable is used as a parameter
Post by: SMcNeill on December 18, 2020, 10:22:42 am
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.

Code: [Select]
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.

Title: Re: Unused variable warning, when variable is used as a parameter
Post by: luke on December 18, 2020, 09:39:02 pm
https://en.m.wikipedia.org/wiki/Variable_shadowing