|
Post by tsh73 on Jul 26, 2018 16:28:13 GMT -5
If formal parameter is in GLOBAL list then factual parameter does not used then calling function - global variable gets called instead.
Here's (simplified) code that made me really puzzled today.
global n n=10
call doSomeTests
input "press ENTER"; a$ end
sub doSomeTests nMax = 100 print "Just print ";n;" random numbers from 0 to ";nMax-1 for i = 1 to n print rand(nMax) 'nMax is not used next end sub
function rand(n) 'now because n is global, global 'n' is used - it is a bug rand = int(rnd(0)*n) end function
Expected output: numbers in range 0..99 Real output: numbers in range 0..9
|
|
|
Post by Brandon Parker on Aug 14, 2018 21:26:43 GMT -5
In my opinion this isn't a bug.
Since Liberty BASIC doesn't have a Global/ Local descriptor for the function definition how would the compiler determine whether or not the code calling the function wants to use the variable being passed or the global variable.
One should not use Global variables inside functions and expect that variable to be considered Local by the function.
This is just my opinion, but I do not believe I would consider this a bug; it is how the language works.
{:0)
Brandon Parker
|
|
|
Post by Carl Gundel on Aug 15, 2018 10:28:01 GMT -5
I think it's a bug.
|
|
|
Post by Rod on Aug 16, 2018 4:56:03 GMT -5
Ah, I thought it wasn't a bug too but I see what Anatoly is saying now.
function rand(n) 'now because n is global, global 'n' is used - it is a bug rand = int(rnd(0)*n) end function
Its not that a global value is passed to the sub its that a global value is not updated when used as a receiver in the sub. In Anatoly's code above you would have expected the global variable n to be updated with the value passed as nMax
|
|
|
Post by Carl Gundel on Aug 16, 2018 9:02:27 GMT -5
Ah, I thought it wasn't a bug too but I see what Anatoly is saying now. function rand(n) 'now because n is global, global 'n' is used - it is a bug rand = int(rnd(0)*n) end function Its not that a global value is passed to the sub its that a global value is not updated when used as a receiver in the sub. In Anatoly's code above you would have expected the global variable n to be updated with the value passed as nMax If a variable is defined as a parameter into the function it cannot be treated as a global. It must be its own local variable of the same name. It's remarkable that this one has escaped our attention for a very long time.
|
|
|
Post by Rod on Aug 16, 2018 11:10:55 GMT -5
Ugg, back to being confused. Global surely means global and so any named global is global. Say we had a convention of capitalising globals. I would not expect Max in the main program or Max in the sub or function to be a different variable. Currently Max is not updated as a receiver, fix that then everything falls into place for me.
|
|
|
Post by Carl Gundel on Aug 16, 2018 12:59:49 GMT -5
Ugg, back to being confused. Global surely means global and so any named global is global. Say we had a convention of capitalising globals. I would not expect Max in the main program or Max in the sub or function to be a different variable. Currently Max is not updated as a receiver, fix that then everything falls into place for me. If I specify a function which has an parameter name of x, that should override the global inside that function. Otherwise how can my program use a function written by someone else if a local variable is accidentally hijacked by a global declaration? The following should produce the number 12. The print test(6) line passes in the value 6, and x should be 6 inside the scope of that function, not 5. So, if we were to decide that it's okay for the global to behave the way it does, then at least some sort of compiler warning would be helpful. global x x = 5 print test(6) end function test(x) test = x*2 end function
|
|
|
Post by Rod on Aug 16, 2018 13:35:33 GMT -5
Naw, global is global. You need a transcending global id, say "Max" then Max is Max no matter where it appears but if you assign it as a receiver in a sub it should take the passed value. Otherwise you don't pass it you simply use it or update it.
So I cannot see how you can assign a global "Max" and then expect subs and functions to ignore its global status.
So still very much confused. But that's getting more common as I age.
|
|
|
Post by Carl Gundel on Aug 16, 2018 13:48:38 GMT -5
Naw, global is global. You need a transcending global id, say "Max" then Max is Max no matter where it appears but if you assign it as a receiver in a sub it should take the passed value. Otherwise you don't pass it you simply use it or update it. So I cannot see how you can assign a global "Max" and then expect subs and functions to ignore its global status. So still very much confused. But that's getting more common as I age. That's why I wrote that if we decide it is not a bug, there should be a compiler warning so when it happens there is no confusion. There's nothing earth shaking about this, because this has been going on a long time and LB is still usable.
|
|