FlexBASIC tips and tricks
ersmith
Posts: 6,068
A thread for some tips and tricks about FlexBASIC.
I'll start off with a really simple one: the handy keyword VAR to both declare a variable and initialize it, e.g.:
var x = 1 var y = "hello"
is basically equivalent to
dim x as integer x = 1 dim y as string y = "hello"
The type of the variable being declared is inferred from the expression used to initialize it.
There is one difference between DIM and VAR: at the top level of the program (not inside any subroutine or function) VAR always declares a local variable, whereas DIM declares a global variable that may be read by subroutines and functions.
Comments
Subroutine and function parameters may have default values. For example:
The default values have to be constant, for now (someday I'd like to expand this).
yes, I wish Chip would add those default values to his Spin.
I find them very useful.
Mike
Another tip: FlexBASIC support multiple return values from functions, and may assign multiple variables in one statement, e.g. you can say
x,y,z = 1,2,3
instead ofx = 1 : y = 2 : z = 3
. The values on the RHS are all evaluated first, sox,y = y,x
may be used to swap x and y.An example:
Shift whenever possible (common knowledge for old-school, I know)
Execution time: 450mS
Execution time: 10,300mS
Multiple "prints" because I sometimes get garbage on the first line or two.
Edit: In fact, try to avoid floats in general.
HI
I found in flexbasic putting a delay at the beginning of the program can give the terminal a time to 'settle'.
In PropBasic I first have to set the serial trans to high- I suspect that after uploading the program and resetting it gets left low sometimes, and then put a 5 second delay to give me time to bring up terraterm (from Genie).
Dave
I started to learning/experimenting with FlexBasic. The conclusion: I will use it instead of Spin except the PASM oriented drivers. This language is much simpler and at the same time more powerful than Spin.
Maybe this is the best place for all the mysterious FlexBasic stuff... I (and others) encountered...
(1) The channel number # for opening files and devices, while described in the manual as 2 to 7 can be 2 to 9 for opening a file. Open #9, then input #9 works, while open #10 doesn't
(2) For a SendRecvDevice the channel # greater than 2 doesn't work
2a: are there any other such "devices"? to use
(3) You can attach SendRecvDevice to the reserved channel #0 without closing this channel and it works. If you close #0 before attaching a device to it, it doesn't work, so if you want to print to your driver instead of standard UART, simply open #0
(4) If you mount something, you can call a lot of C file and directory related functions (for example chdir, but also fopen) and it compiles. If there is no "mount" the compiler doesn't recognize them. I have not tried if these function actually work yet.
Edit: 2 and 3 actually is a bug
@pik33
Truly a syntax that allows one to concentrate on what to do rather than how to do it.