? Commands can "return" variables (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Commands can "return" variables

  • #1 ✎ 1026 snail_ 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 was coding in the wee hours and apparently felt particularly sloppy with the code I was writing. I ended up writing something similar to this. DEF THING VAR T% 'does some stuff RETURN T% END It took me far too long to find the actual issue with this code because the compiler has some trouble deciding what to do sometimes. If you haven't noticed the issue yet, don't feel bad, the parser didn't either. The DEF THING is a command, and the code was written so that it would return a value. Despite the fact that commands can't return anything, this compiles perfectly fine! The bug is this: if you try to use a RETURN statement in a command- or OUT-form DEF to return either a variable or a label, the compiler acts as if it's a bare RETURN statement (you're returning nothing.) This is in fact a compile-time and not a run-time bug because if you enable OPTION STRICT and a variable is specified, it doesn't even check if the variable exists! The compiler just throws out whatever variable you passed to the RETURN. When I say it has to be either a label or a variable, I mean this exactly. Trying to pass a number or a string throws Syntax error, as it should. It should be noted that using a RETURN without an argument is perfectly legal in a command or OUT; it simply exits the DEF, as with a GOSUB. Upon execution, the DEF behaves as you would expect, because commands don't return anything and the compiler just throws out the return value. Posted
  • #2 ✎ 1785 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself This isn't actually a bug. What happens is that RETURN in a function without a return value doesn't accept any arguments. RETURN T% is actually being interpreted as RETURN:T%, where T% is a function call (even though the function T% probably doesn't exist) The reason this doesn't cause an error is because it doesn't check if T% is a valid function until it reaches it, which never happens because the RETURN. Posted Edited by 12Me21
  • #3 ✎ 175 DFrost Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength cool! Posted