SX/B Sub Question
Hello,
·I am trying to understand SX/B a little better. I have writen basic programs and declared subs like "Runmotor····· Sub" And it works fine. When I look at examples written by experienced programers I see them use "Runmotor··· Sub··· 1,2" I don't see what the '1,2' means and why one would add that. The SX/B help file doesn't seem to explain this. Are these optional parms?
Thanks,
Jim W
·I am trying to understand SX/B a little better. I have writen basic programs and declared subs like "Runmotor····· Sub" And it works fine. When I look at examples written by experienced programers I see them use "Runmotor··· Sub··· 1,2" I don't see what the '1,2' means and why one would add that. The SX/B help file doesn't seem to explain this. Are these optional parms?
Thanks,
Jim W
Comments
The reason for the number is that it tells the compiler how many (possible) bytes you can pass to the subroutine. In your question, the "1, 2" means that the subroutine expects at least one byte, but could handle two.
A good use for subroutines is to encapsulate SX/B keywords that generate a lot of code; PAUSE, for example. By putting PAUSE into a subroutine it only gets compiled once. By allowing variable parameters the subroutine can be as flexible as the original keyword. This subroutine handles PAUSE and will accept byte or word values.
Note that if your subroutine doesn't require any parameters you should specify zero.
The help file didn't explain it anywhere near as well as you did. I can see clearly now.
Best,
Jim W.
Subroutine Declaration
The programming and use of subroutines is simplified by declaring the subroutine name and the parameter(s) (if any) required. Additionally, the declaration of subroutines allows them to return a byte value (see FUNC, below, for return word values). Declaring subroutines offers significant advantages to the programmer:
The compiler can check code for the correct number of parameters
GOSUB is no longer required to call the subroutine
The subroutine can return a direct (byte) value
-- This is simpler than passing the variable's address (@someVar)
Code page management is automatically handled
SX/B subroutines are defined using the following syntax:
Label SUB {Min{, Max}}
Where Min is the minimum number of required parameters (if any) and Max is the maximum number of parameters passed to the subroutine. If Max is not specified then Min is the fixed number of parameters allowed.