LoginLogin

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
END
Output: 42
OPTION STRICT

COMMON DEF FUNCTION()
VAR R=G
RETURN R
END

'Here the variable declaration comes after the function.
VAR G=42
?FUNCTION()
END
Output: Undefined variable in 0:4 (EDIT: Forgot to add VAR to variable declarations.)

It's not a bug, really. G didn't exist outside the function when the function was created, so it assumed it was a local variable. It's a good idea to put all your functions at the end of your code to avoid this.

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...

I actually thought variables in functions inherited the values set outside the function, and when they're changed, they would revert back to their original value, given that it's a numerical variable. I might have to edit every function in DEFY to declare the variables...

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
END
Output: 42
OPTION STRICT

COMMON DEF FUNCTION()
VAR R=G
RETURN R
END

'Here the variable declaration comes after the function.
VAR G=42
?FUNCTION()
END
Output: 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."?

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".

"Variable declaration order is based on position in code regardless of functions" is good

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.