SX/B - FUNCTIONS vs SUBROUTINES
T&E Engineer
Posts: 1,396
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
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
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:
You would capture the return value like this:
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.
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:
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
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?
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:
·
So:
could also be just:
.. 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.