? RETURNing from GOSUB inside a function does not work correctly (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password

RETURNing from GOSUB inside a function does not work correctly

  • #1 ✎ 1483 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website On the other thread, someone idiot pointed out that RETURN doesn't work as you'd expect in functions that return things: DEF EXAMPLE() GOSUB @TEST RETURN "666" @TEST RETURN END PRINT EXAMPLE()This gives an uninitialized variable used error at the second RETURN. However, if you give a value that is the same type as the real return value: DEF EXAMPLE() GOSUB @TEST RETURN "666" @TEST RETURN "" END PRINT EXAMPLE()Then it will work just as expected. Posted Edited by 12Me21
  • #2 ✎ 87 banana RNG Hidden Easter Eggs Disguised Hidden Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength someone? you bastard Posted
  • #3 ✎ 1483 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website Ok, I finally figured out why this happens. When RETURN is used in a function that returns a value (That is, a function defined as DEF NAME(...)), it does two things. First it sets the value of a hidden OUT variable, and then it exits the function. DEF TEST() RETURN 6 END 'this is the same as: DEF TEST OUT X X=6 RETURN END When making the changes to allow RETURN to return from a GOSUB if nessesary, they stopped it from exiting the function, but it still sets the value of that hidden OUT variable! OUT variables can only have their type changed once, so this causes a type mismatch: DEF EXAMPLE() GOSUB @TEST RETURN 7 'type mismatch @TEST RETURN "HELLO" END Is the same as DEF EXAMPLE OUT X GOSUB @TEST X=7 'can't make X into a number because it's a string now RETURN 'return from function @TEST X="HELLO" RETURN 'return from GOSUB END Posted
  • #4 ✎ 476 SquareFingers
    someone idiot
    Well, that's rather unkind.
    Posted