Shop OBEX P1 Docs P2 Docs Learn Events
functions & program flow for a noob — Parallax Forums

functions & program flow for a noob

ArchiverArchiver Posts: 46,084
edited 2003-07-18 03:58 in General Discussion
Hello...

I am very used to program flow in
php/javascript/java/etc, but I am unsure how to
accomplish similar things in PBASIC.

What techniques are used to pass values to functions?
How are functions constructed in PBASIC? Say I have
an associative array (I know this is in improper
lingo) and want to sent a key and that array to a
function, receiving back the value contained within
the key.

Is there an equivalent in PBASIC? Am I thinking
backwards here? Pipe dreams? Gimme the scoop,
thanks!

--t

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-07-17 09:54
    Hi

    For something like associative arrays (well, rather a lookup table)
    see the stamp manual for the commands LOOKUP and LOOKDOWN.

    PBasic does not provide local variables or argument passing to
    functions, it does not even provide functions to be exact. There are
    several ways to pass arguments to subroutines: global variables, the
    scratchpad RAM (BS2xx only) or the EEPROM storage (only approriate
    for mostly constant parameters).
    Don't forget you have only 26 byte of RAM.

    subroutines (as a replacement for functions) in PBasic have the
    folowing form:

    'subroutine call
    gosub label
    ...

    'a subroutine
    label:
    'subroutine code
    return

    Regards
    Adrian
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-17 15:12
    The following applies to PBasic 2.0.
    PBasic 2.5, currently in Beta, has
    better control flow (IF...THEN).

    PBasic 2.0 'function' implementation:
    1. PBasic only has GOTO and GOSUB.
    (OK, BRANCH is computed GOTO)
    2. PBasic has NO 'local' variables
    (Variables local to a function, allocated on
    the stack).
    3. PBasic only allows you 4 nested levels of GOSUB.
    4. PBasic has NO 'stack' that you can use.
    5. PBasic 'IF..THEN' is only a conditional GOTO.
    6. You have 26 BYTEs of Register space for variables.
    -- These can be allocated in ANY combination of
    -- BIT, NIB, BYTE, or WORD (ALL UNSIGNED)
    -- 1, 4, 8, 16 bits each.

    So:
    Temp1 VAR BYTE
    Temp2 VAR BYTE
    Temp3 VAR BYTE

    Parm1 VAR BYTE
    Parm2 VAR BYTE
    Result VAR BYTE

    MAIN:
    Temp1 = 5
    Temp2 = 6
    GOSUB MyFunc ' Uses 'Temp1' and 'Temp2' to make 'Temp3'
    IF Temp3 = 0 THEN Skip1 ' implied: GOTO Skip1
    ' ELSE
    Result = Temp3
    Skip1:
    GOTO MAIN
    END

    '***** Subroutines ****
    MyFunc:
    Temp3 = Temp1 + Temp2
    RETURN

    If you do this, be very careful if 'MyFunc' calls
    another subroutine, don't assume it has its 'own'
    Temp1, Temp2, and Temp3. These are GLOBALS.

    --- In basicstamps@yahoogroups.com, "mr. t" <nadanada@e...> wrote:
    > Hello...
    >
    > I am very used to program flow in
    > php/javascript/java/etc, but I am unsure how to
    > accomplish similar things in PBASIC.
    >
    > What techniques are used to pass values to functions?
    > How are functions constructed in PBASIC? Say I have
    > an associative array (I know this is in improper
    > lingo) and want to sent a key and that array to a
    > function, receiving back the value contained within
    > the key.
    >
    > Is there an equivalent in PBASIC? Am I thinking
    > backwards here? Pipe dreams? Gimme the scoop,
    > thanks!
    >
    > --t
    >
    > __________________________________
    > Do you Yahoo!?
    > SBC Yahoo! DSL - Now only $29.95 per month!
    > http://sbc.yahoo.com
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-18 03:58
    Good to know that about gosub, Allan, thanks! May have been the
    cause of previous unsolved mysteries...
    To add my 2 cents...
    If something doesn't work, there are no exception throws, the
    program dumps to the next command (memory address?).
    Let the 'debug' statement be your dear friend for showing you what
    is happening.
    Negative (signed) numbers must be words. I'd steer clear of them
    all together if you can...

    You maybe thinking the Javelin stamp is more up your alley - You
    have to use their 'special subset' of java - very limited. I had a
    bad experience with them and trying to pass serial data... you need
    the special Javelin carrier board to do serial comm (which isn't
    sold outside the kit).

    -Damon

    --- In basicstamps@yahoogroups.com, "Allan Lane" <allan.lane@h...>
    wrote:

    > 3. PBasic only allows you 4 nested levels of GOSUB.
Sign In or Register to comment.