LoginLogin
Might make SBS readonly: thread

RETURNing from GOSUB inside a function does not work correctly

Root / SmileBASIC Bug Reports / [.]

12Me21Created:
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.

someone?
Spoileryou bastard

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

someone idiot
Well, that's rather unkind.