? Variables act empty inside functions that are placed before that variable's creation. (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password
This website has been moved, please see this post

Variables act empty inside functions that are placed before that variable's creation.

  • #1 ✎ 90 HackTheWorlds Second Year My account is over 2 years old Website Summer 2016 Contest Participant I participated in the SmileBASIC Source Summer 2016 Contest! Programming Contest Reading I like to read books! Hobbies 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.) Posted Edited by HackTheWorlds
  • #2 ✎ 1882 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself 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. Posted Edited by 12Me21
  • #3 ✎ 90 HackTheWorlds Second Year My account is over 2 years old Website Summer 2016 Contest Participant I participated in the SmileBASIC Source Summer 2016 Contest! Programming Contest Reading I like to read books! Hobbies 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. Posted
  • #4 ✎ 1130 snail_ Power User QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! Helper Received for being very helpful around SmileBASIC Source Achievements Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements 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? Posted
  • #5 ✎ 72 calc84maniac OSP Contest 1 Winner I won the first SmileBASIC Source OSP Contest! Programming Contest Amazing Page Hidden Achievements Scholar Received for knowing a great deal about programming topics Achievements 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... Posted
  • #6 ✎ 246 Avery First Week Joined in the very first week of SmileBASIC Source Website Third Year My account is over 3 years old Website Disguised Hidden Website 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... Posted
  • #7 ✎ 477 SquareFingers
    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
    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 ✎ 90 HackTheWorlds Second Year My account is over 2 years old Website Summer 2016 Contest Participant I participated in the SmileBASIC Source Summer 2016 Contest! Programming Contest Reading I 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 ✎ 1882 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I 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 ✎ 74 RaichuBender Avatar Taboo I didn't change my avatar for 180 days Website Avatar Embargo I didn't change my avatar for 90 days Website Avatar Block I didn't change my avatar for 30 days. Website 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. Posted Edited by RaichuBender