rfobasic.freeforums.org
http://rfobasic.freeforums.org/

Minor suggestion for use of pre/postfix operators
http://rfobasic.freeforums.org/minor-suggestion-for-use-of-pre-postfix-operators-t5011.html
Page 1 of 1

Author:  ReNoob [ Sat Apr 15, 2017 9:29 am ]
Post subject:  Minor suggestion for use of pre/postfix operators

I recently wrote a short function that returns four 8-bit values. But of course a function can only return one value. Easy enough to return as a single 32-bit number with shift and add instructions. Then main program just extracts the values in the opposite way they were packed:

Code:
DO
   a[++v]=BAND(x,255)
   x=SHIFT(x,8)
UNTIL v=4


Simple, right? Until I went to print them out with this routine:
Code:
DO
  PRINT RIGHT$("0"+HEX$(a[v--]),2);" | ";
UNTIL v=0


Which only printed the second and fourth values. This drove me nuts until I read the manual on pre and postfix operators, which referred me to "User Defined Functions", which largely clarified everything. I simply took out the postfix and put a v-=1 before the until. My suggestion is, instead of referring the reader to "User Defined Functions", shouldn't it just tell you in the pre- and postfix section, that they don't work within any function, not just the user defined? Just a thought.
Peace,

ReNoob

Author:  humpty [ Mon Apr 17, 2017 12:26 pm ]
Post subject:  Re: Minor suggestion for use of pre/postfix operators

The pre/post fix Ops you used did work.
The problem is with the PRINT command which evaluates functions twice during parsing.
(see "Print with User-Defined Functions" in the manual)

The User-Defined-Functions relationship with function calls you referred to is something different.
When you call a function by reference, it expects a variable. If you pre/post Op it, it becomes a
value (which cannot be passed by reference).

Author:  ReNoob [ Mon Apr 17, 2017 2:43 pm ]
Post subject:  Re: Minor suggestion for use of pre/postfix operators

You were correct about the print statement, of course. Codewise though, I feel that making an assignment/print out of it ("Z$=RIGHT$(......), followed by PRINT Z$;" |"; is less efficient than just eliminating the postfix. I'm still just the student here, I could be wrong.
Peace
'Noob

Page 1 of 1 All times are UTC - 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/