Shop OBEX P1 Docs P2 Docs Learn Events
FlexBASIC tips and tricks — Parallax Forums

FlexBASIC tips and tricks

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:

    sub greet(a = "hello" as string, b = "goodbye" as string)
       print a; " ... ";
       print b
    end sub
    
    greet                         ' prints "hello ... goodbye"
    greet "hi"                    ' prints "hi ... goodbye"
    greet "bonjour", "au revoir"  ' prints "bonjour ... au revoir"
    

    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 of x = 1 : y = 2 : z = 3. The values on the RHS are all evaluated first, so x,y = y,x may be used to swap x and y.

    An example:

    '
    ' multiple assignment examples
    '
    dim as string x, y
    dim as integer n
    
    '
    ' trivial function that doubles a string
    ' and returns the length of the new string, plus the string
    '
    function multireturn(x as string) as integer,string
      x = x + x
      var n = len(x)
      return n, x
    end function
    
    ' start by demonstrating multiple return values
    ' calculate both the new string (placed in x) and its length (placed in n)
    n,x = multireturn("abc")
    print "new string is: ["; x; "] and has length "; n
    
    ' now multiple assignment
    x = "hello"
    y = "goodbye"
    
    print "x=["; x; "] and y=["; y; "]"
    x,y = y,x
    print "x=["; x; "] and y=["; y; "]"
    
  • MicksterMickster Posts: 2,698
    edited 2021-12-22 10:00

    Shift whenever possible (common knowledge for old-school, I know) :smile:

    dim as long x,y,a,i,tmr,tmr2
    
    x=3000
    y=2000
    
    tmr=getms()
    for i = 1 to 1_000_000
        a=(x+y) shr 1
    next
    tmr2=getms()
    
    
    print" "
    print" "
    print"a= "; a
    print tmr2-tmr
    

    Execution time: 450mS

    dim as long x,y,a,i,tmr,tmr2
    
    x=3000
    y=2000
    
    tmr=getms()
    for i = 1 to 1_000_000
        a=(x+y)*0.5
    next
    tmr2=getms()
    
    
    print" "
    print" "
    print"a= "; a
    print tmr2-tmr
    
    

    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.

  • tritoniumtritonium Posts: 543
    edited 2021-12-22 10:46

    HI

    Multiple "prints" because I sometimes get garbage on the first line or two.

    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

  • pik33pik33 Posts: 2,388
    edited 2021-12-27 19:26

    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. :+1:

Sign In or Register to comment.