Shop OBEX P1 Docs P2 Docs Learn Events
SX/B Sub Question — Parallax Forums

SX/B Sub Question

Basic JimBasic Jim Posts: 106
edited 2007-05-12 20:50 in General Discussion
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

Comments

  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-10 21:40
    SUBs and FUNCs are covered in the Definitions section of the help file.

    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.

    ' Use: DELAY ms
    ' -- 'ms' is delay in milliseconds, 1 - 65535
    
    DELAY:
      IF __PARAMCNT = 1 THEN
        tmpW1 = __PARAM1                            ' save byte value
      ELSE
        tmpW1 = __WPARAM12                          ' save word value
      ENDIF
      PAUSE tmpW1 
      RETURN
    
    



    Note that if your subroutine doesn't require any parameters you should specify zero.
  • Basic JimBasic Jim Posts: 106
    edited 2007-05-10 21:47
    Thanks JonnMac,

    The help file didn't explain it anywhere near as well as you did. I can see clearly now.

    Best,
    Jim W.
  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-05-10 21:55
    That's my fault since I wrote the help file -- but it was a while ago and we always get better with age, right?
  • John KauffmanJohn Kauffman Posts: 653
    edited 2007-05-12 20:50
    Here is a section from the SX/B help "SX/B Definitions." An example follows in the help.


    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.
Sign In or Register to comment.