Variables act empty inside functions that are placed before that variable's creation.
#1✎ 90HackTheWorldsSecond YearMy account is over 2 years oldWebsiteSummer 2016 Contest ParticipantI participated in the SmileBASIC Source Summer 2016 Contest!Programming ContestReadingI like to read books!HobbiesBasically, 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: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
(EDIT: Forgot to add VAR to variable declarations.)
Posted
Edited
by HackTheWorlds
#2✎ 188212Me21Syntax HighlighterReceived for creating the code syntax highlighter on SBSNight PersonI like the quiet night and sleep late.Express YourselfIt'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.
Posted
Edited
by 12Me21
#3✎ 90HackTheWorldsSecond YearMy account is over 2 years oldWebsiteSummer 2016 Contest ParticipantI participated in the SmileBASIC Source Summer 2016 Contest!Programming ContestReadingI like to read books!HobbiesOk. 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.
Posted
#4✎ 1130snail_Power UserQSP Contest 1 Contest ParticipantI participated in the first SmileBASIC Source QSP Contest!HelperReceived for being very helpful around SmileBASIC SourceAchievementsAmazing ContributorSomeone thinks I'm an awesome person who has done so much for the community!AchievementsI 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?
Posted
#5✎ 72calc84maniacOSP Contest 1 WinnerI won the first SmileBASIC Source OSP Contest!Programming ContestAmazing PageHiddenAchievementsScholarReceived for knowing a great deal about programming topicsAchievementsInteresting, 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...
Posted
#6✎ 246AveryFirst WeekJoined in the very first week of SmileBASIC SourceWebsiteThird YearMy account is over 3 years oldWebsiteDisguisedHiddenWebsiteI 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...
Posted
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: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."?
Posted
#8✎ 90HackTheWorldsSecond YearMy account is over 2 years oldWebsiteSummer 2016 Contest ParticipantI participated in the SmileBASIC Source Summer 2016 Contest!Programming ContestReadingI like to read books!Hobbies
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".
Posted
#9✎ 188212Me21Syntax HighlighterReceived for creating the code syntax highlighter on SBSNight PersonI like the quiet night and sleep late.Express Yourself"Variable declaration order is based on position in code regardless of functions" is good
Posted
Edited
by 12Me21
#10✎ 74RaichuBenderAvatar TabooI didn't change my avatar for 180 daysWebsiteAvatar EmbargoI didn't change my avatar for 90 daysWebsiteAvatar BlockI didn't change my avatar for 30 days.WebsiteWow, 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.
Posted
Edited
by RaichuBender