There’s not a lot of differences in SUB and GOSUB routines, but I find them significant. Biggest things to remember:
* SUB/FUNCTION are self-contained; GOSUB aren’t.
GOSUB Example:
x = 3
PRINT x
GOSUB AddOne
PRINT x
END
AddOne:
x = x + 1
PRINT x
RETURN
The above will print 3, 4, 4 as the three lines of results.
SUB example:
x = 3
PRINT x
AddOne
PRINT x
END
SUB AddOne
x = x + 1
PRINT x
END SUB
Now here, we’ll see 3, 1, 3 as the result. AddOne is self-contained as a SUB, and x inside it has no relation to the value of x outside it. When we call AddOne, SUB_x has a default value of 0, adds 1 to that, and SUB_x becomes 1. The Main_x we set at a value of 3, and it never changes.
This “self-contained” feature of SUB/FUNCTION makes them very suitable for creating libraries which we can then $INCLUDE into other programs. GOSUB tends to be very Program specific, as its variables can affect the rest of your code, whereas SUB/FUNCTION doesn’t.
*SUB/FUNCTION have passable parameters; GOSUB doesn’t.
Since a SUB is self-contained, we need some way to pass values back and forth to it — the most basic way is via parameters.
x = 3
PRINT x
AddOne x
PRINT x
AddOne x
PRINT x
END
SUB AddOne (x)
x = x + 1
END SUB
And here, we print 3, 4, 5. We pass the value of x back and forth in the parenthesis when we declare our SUB. (The (x) in this case.)
And that’s the very basics, to highlight the differences and help get you started on learning how to use them. Just post when interested in learning more, and ask any questions you might have, and I — and the rest of the forum members, I’m certain — will be happy to help any way we can with your concerns. ;D