Variables act empty inside functions that are placed before that variable's creation.
Root / Programming Questions / [.]
HackTheWorldsCreated:
Basically, depending on where the code for a function is placed in the physical code, it will act as if certain variables haven't been created. If a function it placed below the variable declaration, it will behave as expected. If it is placed above the variable declaration, the function will treat the variable as if it didn't exist. It is seen better when OPTION STRICT is enabled because it throws a "undefined variable" error, but it is not necessary, nor does the variable need to be declared explicitly. It happens with strings and values, real and integer.
Here are some examples:
OPTION STRICT 'Here, the variable declaration is before the function. VAR G=42 ?FUNCTION() END COMMON DEF FUNCTION() VAR R=G RETURN R ENDOutput: 42
OPTION STRICT COMMON DEF FUNCTION() VAR R=G RETURN R END 'Here the variable declaration comes after the function. VAR G=42 ?FUNCTION() ENDOutput: Undefined variable in 0:4 (EDIT: Forgot to add VAR to variable declarations.)
Ok. That makes sense. Not really a bug, more of a quirk. And I do generally put my functions after everything else, it was just a special case that I found this.
I had thought that the precompiler ropes up all of the variables and allocs them beforehand (without strict), regardless of order. It's really not noticeable when you don't have strict on, but it never occurred to me that the allocation is deferred until they appear. Could the COMMON DEF have anything to do with this?
Interesting, I had assumed that all variables not explicitly declared in a function were assumed to be global. Wasn't expecting implicit local variables to be a thing...
Here are some examples:So when you say, "Variables act empty inside functions that are placed before that variable's creation.", you mean "Variables are undeclared before they are declared."?OPTION STRICT 'Here, the variable declaration is before the function. VAR G=42 ?FUNCTION() END COMMON DEF FUNCTION() VAR R=G RETURN R ENDOutput: 42OPTION STRICT COMMON DEF FUNCTION() VAR R=G RETURN R END 'Here the variable declaration comes after the function. VAR G=42 ?FUNCTION() ENDOutput: Undefined variable in 0:4
So when you say, "Variables act empty inside functions that are placed before that variable's creation.", you mean "Variables are undeclared before they are declared."?Yes, sort of. When I say "functions that are placed before that variable's creation" I mean they are placed physically above the variable declaration in the code. It seems like it would still work, because you might think that variables inside a function are evaluated last, but that is not the case. So here's another way I could of said it "Variables act undeclared inside functions if placed above their declaration in the code" or "Variable declaration order is based on position in code regardless of functions".
Wow, now that I know this, I can fix a bug that I've been having for a long time. I couldn't find what was causing the bug. Apparently I didn't declare the variable beforehand.