Every programmer worth their salt knows the difference between LOCAL and GLOBAL variables.
But I was wondering, what if you could reverse that ?
That is, when you define a function, you have the option of saying GLOBAL in there, either to define a global variable, or just to make a flag change.
Because by doing so ANY variables at that point which are not issued as GLOBAL automatically default to LOCAL.
For programs that do not use too many global variables, this might be an easier way to code as you would not have to assign each and every local variable for each and every function.
Thoughts ?
Doesn't pico already do this?
This:
function fun() x=555 print("inside call x=") print(x) end print("before call x=") print(x) fun() print("after call x=") print(x) |
outputs this:
BEFORE CALL X= NIL INSIDE CALL X= 555 AFTER CALL X= 555 |
Looks like a variable declared inside a function with out explicit "local" is already global.
Which is not good. Try this example:
function main() cls() a=4 one() print(a) end function one() print(a)-- should be null end main()--to top |
If locals are automatically defined by default, then you can see that NIL should be printed a we are inside a separate function.
But it does not.
function main() cls() one() print(a)-- should be null end function one() a=4 print(a) end main()--to top |
Both programs treat "A" as a global variable.
I was hoping that any variables not specifically declared inside a function would be LOCAL, but only if you could define GLOBAL as well. Neither of which is correct.
... which means I will need to continue to define each and every local variable with care.
[Please log in to post a comment]