Shop OBEX P1 Docs P2 Docs Learn Events
SX/B - FUNCTIONS vs SUBROUTINES — Parallax Forums

SX/B - FUNCTIONS vs SUBROUTINES

T&E EngineerT&E Engineer Posts: 1,396
edited 2008-11-12 09:29 in General Discussion
Can someone explain these a bit clearer than the SX/B help?

I beleive I understand it generally but still not clear on when to use one vs the other. What is the proper format and layout of each too. To me a Function is always a subroutine but a subroutine is not necessarily a function. Why not just have subroutines then? Am I missing something?

Thanks

Comments

  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-06-19 13:31
    In any programming language, subroutines tend to be used to perform some process while functions are used to perform some process and then return a value. In SX/B you can have a subroutine return one byte, but this is no longer "good form" (my opinion); if your process is to return a value as its standard operation then it should be designed (and declared) as a function. With SX/B a function can return up to five bytes.

    There is a bit of a trick when a function passes back more than two bytes: you have to manually capture bytes three and higher. If, for example you have a function that returns four bytes (e.g., 32-bit result) -- like this:

    RETURN tmpW1, tmpW2
    ENDFUNC
    



    You would capture the return value like this:

    resultLo = BIGMULT
    resultHi = __WPARAM34
    
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-06-19 14:49
    OK thanks JonnyMac.

    So it's all about whether you need to pass variables back and forth. That make's sense. I have not written any functions for SX/B and have only used subroutines and also some of your functions.

    Thanks.
  • JonnyMacJonnyMac Posts: 9,214
    edited 2007-06-19 15:00
    No, it's about whether a chunk of code is designed to return a value -- that's a function; a subroutine may accept a parameter but [noparse][[/noparse]usually] doesn't return anything (if it does it is limited to one byte). Here are two very simple examples:

    TX_BYTE         SUB      1
    RX_BYTE         FUNC     1, 0
    



    Note that in the FUNC declaration a single byte is returned (that's the first number), but no parameters (second value) are passed to it. TX_BYTE is expecting a single byte -- as a SUB it returns nothing. Here's how these are coded:

    SUB TX_BYTE
      SEROUT TX, BaudMode, __PARAM1
      ENDSUB
    
    FUNC RX_BYTE
      SERIN RX, BaudMode, tmpB1
      RETURN tmpB1
      ENDFUNC
    



    Note that -- in this [noparse][[/noparse]SX/B v2.0 compliant] form -- the RETURN keyword is only used with the function.

    Post Edited (JonnyMac) : 6/19/2007 3:05:25 PM GMT
  • beatsbeats Posts: 17
    edited 2008-11-12 01:50
    ·A question concerning the FUNCTION command....
    the value that is "returned" at the end of a FUNCTION, is this value retreived by equating a variable to the function if you were to call it somewhere else in the program?
    for example, continuing the small snipet of code above me^,· would the following be true?



    tmpB2 = RX_BYTE        ' therefore, would the [u]VALUE[/u] from tmpB1 now be copied into tmpB2?
     
    



    also if this is correct, is it also acceptable to just use the same variable when retreiving the returned "value"? (assuming these are global variables)
    e.g:
    tmpB1 = RX_BYTE        '
    


    ·
  • VonSzarvasVonSzarvas Posts: 3,526
    edited 2008-11-12 09:29
    When I tried it, I found that the variable (provided global) would already contain the value...

    So:

    tmpB1 = RX_BYTE
    WATCH tmpB1        '
    



    could also be just:

    RX_BYTE
    WATCH tmpB2
    


    .. ie. watch the temp value used in the function, and consume it in your code.

    This is madly confusing perhaps, and great care should be taken that the tmp variable is not altered by another routine.

    This principle would apply in the case of a SUB or FUNC.

    But as JonnyMac said; that this is "bad form". It does not make for portable code and can often lead to errors due to other parts of the program using the same tmp vars. Although, applied with care, it would appear perfectly valid and may be a useful technique if compacting and saving on variable space becomes necessary.
Sign In or Register to comment.