|
Post by Janet on Dec 22, 2007 14:03:19 GMT -5
As a general rule, I don't use GLOBAL much. I prefer to store global variables in an array. This may be well known to those who use GLOBAL more often. I just stumbled across this.
Setting a variable to GLOBAL, if that variable is also the name of a Function, will cause the variable to be 0 at all times, both inside the function itself and upon return from the function.
Global randomNumber ' Remove this line to see difference Cls randomNumber = randomNumber() Print "Return from function: randomNumber = ";randomNumber End
Function randomNumber() randomNumber = Int(Rnd(1) * 25) + 1 Print "Inside function: randomNumber = ";randomNumber End Function
Again, I may have just stumbled across a practice that more experienced users would shun from the start. A similar event happens in Liberty BASIC. The only difference is that inside the sub, the value is as set, then upon return, the value resets to 0. I checked the Help File in Liberty BASIC and couldn't find anything there that said to avoid setting a Function variable as global either.
I'm not suggesting this is a bug. I'm just thinking a mention should be made in the LB, JB, and Run BASIC documentations.
|
|
|
Post by carlgundel on Dec 22, 2007 15:06:01 GMT -5
As a general rule, I don't use GLOBAL much. I prefer to store global variables in an array. This may be well known to those who use GLOBAL more often. I just stumbled across this. Setting a variable to GLOBAL, if that variable is also the name of a Function, will cause the variable to be 0 at all times, both inside the function itself and upon return from the function. Global randomNumber ' Remove this line to see difference Cls randomNumber = randomNumber() Print "Return from function: randomNumber = ";randomNumber End
Function randomNumber() randomNumber = Int(Rnd(1) * 25) + 1 Print "Inside function: randomNumber = ";randomNumber End Function
Again, I may have just stumbled across a practice that more experienced users would shun from the start. A similar event happens in Liberty BASIC. The only difference is that inside the sub, the value is as set, then upon return, the value resets to 0. I checked the Help File in Liberty BASIC and couldn't find anything there that said to avoid setting a Function variable as global either. I'm not suggesting this is a bug. I'm just thinking a mention should be made in the LB, JB, and Run BASIC documentations. Thanks Janet. I'm not sure what I think about this yet, but thanks for reporting it. I'm probably not going to call that a showstopper, but perhaps I should document it. -Carl
|
|
|
Post by carlgundel on Dec 22, 2007 15:54:42 GMT -5
Actually I've decided to put a compile-time check in there to prevent declaration of globals that match function names.
Thanks again,
-Carl
|
|