LoginLogin

GOSUB within functions?

Root / Programming Questions / [.]

Gaelstrom_ValenceCreated:
The manual says it can't be used, but it seems to work fine for me? Is it referring to something else, or is the wording there a bit messed up?

It originally wasn't possible but they fixed it in version 3.5.2 or something.

Even if it works now, your really shouldn't be mixing GOSUB and functions, just use functions.

Even if it works now, your really shouldn't be mixing GOSUB and functions, just use functions.
This came up because I wanted to see if I could use ON within functions. I ended up going with GOTO anyways, but in either case... Well, I'm not sure if my speed test thing is accurate... but as far as I know, both GOSUB and GOTO with the ON instruction are faster than:
DEF THING CTRL

IF CTRL==0 THEN FUNC1

IF CTRL==1 THEN FUNC2

IF CTRL==2 THEN FUNC3

IF CTRL==3 THEN FUNC4

You could use ELSEIF for that But ON/GOTO/GOSUB is slightly faster Even without GOSUB, you can use another GOTO instead of RETURN:
DEF THING CTRL
 VAR X
 ON CTRL GOTO @0,@1,@2,@3
 @RET
 PRINT X
 RETURN
 @0
  X=0
 GOTO @RET
 @1
    ...
END
But you should only do this in extreme circumstances when you need a tiny bit more speed (the speed difference between this and ON GOSUB is very small)