Log In  

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 ?

P#30902 2016-10-13 23:36 ( Edited 2016-10-14 17:49)

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.

P#30932 2016-10-14 13:26 ( Edited 2016-10-14 17:27)

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.

P#30935 2016-10-14 13:49 ( Edited 2016-10-14 17:50)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 21:52:37 | 0.006s | Q:11