Shop OBEX P1 Docs P2 Docs Learn Events
Any way to send variables to a function/procedure — Parallax Forums

Any way to send variables to a function/procedure

ArchiverArchiver Posts: 46,084
edited 2001-10-02 14:57 in General Discussion
While the gosub command is great for reusing code, it would be
WONDERFUL to be able to send variables to them. Does anyone know if
this can be done? Are variables always global? For example I'd like
to do this:

MAIN:
variable1 ='code here to get a value from a pin
variable2 = doNumberCrunching(variable1)
goto Main

doNumberCrunching(aNumber)
aNumber = aNumber + 1
doNumberCrunching = aNumber


If you've programmed in C or vbscript, or regular basic, you know
what I mean.

I'm pretty sure subs can't take variables, but is there any kind of
work around?

Thanks!

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-10-02 04:09
    Your talking procedure based syntax, thats far out of
    reach for the stamp.



    --- nospam@e... wrote:
    > While the gosub command is great for reusing code,
    > it would be
    > WONDERFUL to be able to send variables to them.
    > Does anyone know if
    > this can be done? Are variables always global? For
    > example I'd like
    > to do this:
    >
    > MAIN:
    > variable1 ='code here to get a value from a pin
    > variable2 = doNumberCrunching(variable1)
    > goto Main
    >
    > doNumberCrunching(aNumber)
    > aNumber = aNumber + 1
    > doNumberCrunching = aNumber
    >
    >
    > If you've programmed in C or vbscript, or regular
    > basic, you know
    > what I mean.
    >
    > I'm pretty sure subs can't take variables, but is
    > there any kind of
    > work around?
    >
    > Thanks!
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed.
    > Text in the Subject and Body of the message will be
    > ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >


    __________________________________________________
    Do You Yahoo!?
    Listen to your Yahoo! Mail messages from any phone.
    http://phone.yahoo.com
  • ArchiverArchiver Posts: 46,084
    edited 2001-10-02 04:17
    There is a way to "trick" the stamp into this sort of procedure. However it is
    more work than good sometimes. If you are using a SX
    serries stamp you can use scratchpad to create a stack. In C or ASM when you
    "pass variables" you are usually pushing the pointers
    or variables onto a stack then the function pops them off. We had an application
    that required iteration, hence stack based calling.
    We wrote a simple set of routines that would pop data from the "stack" (scratch
    pad). Just a thought. This is a LONG work around and
    may be more trouble than it is worth to you.

    MH


    Original Message
    From: <nospam@e...>
    To: <basicstamps@yahoogroups.com>
    Sent: Monday, October 01, 2001 12:46 PM
    Subject: [noparse][[/noparse]basicstamps] Any way to send variables to a function/procedure


    > While the gosub command is great for reusing code, it would be
    > WONDERFUL to be able to send variables to them. Does anyone know if
    > this can be done? Are variables always global? For example I'd like
    > to do this:
    >
    > MAIN:
    > variable1 ='code here to get a value from a pin
    > variable2 = doNumberCrunching(variable1)
    > goto Main
    >
    > doNumberCrunching(aNumber)
    > aNumber = aNumber + 1
    > doNumberCrunching = aNumber
    >
    >
    > If you've programmed in C or vbscript, or regular basic, you know
    > what I mean.
    >
    > I'm pretty sure subs can't take variables, but is there any kind of
    > work around?
    >
    > Thanks!
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and Body
    of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2001-10-02 08:35
    I was wondering before why parallax couldn't have built some intelligent
    things like complex branching (if x then instruction, for example, rather
    than trying to construct a bunch of labels) and functions-with-arguments
    into the preprocessor, without changing the stamp as it is in the
    slightest. It would certainly make my Object-oriented sensibilities a
    little happier, rather than constantly writing spaghetti.

    On Mon, 1 Oct 2001, Michael Hendricks wrote:

    > There is a way to "trick" the stamp into this sort of procedure. However it is
    more work than good sometimes. If you are using a SX
    > serries stamp you can use scratchpad to create a stack. In C or ASM when you
    "pass variables" you are usually pushing the pointers
    > or variables onto a stack then the function pops them off. We had an
    application that required iteration, hence stack based calling.
    > We wrote a simple set of routines that would pop data from the "stack"
    (scratch pad). Just a thought. This is a LONG work around and
    > may be more trouble than it is worth to you.
    >
    > MH
    >
    >
    >
    Original Message
    > From: <nospam@e...>
    > To: <basicstamps@yahoogroups.com>
    > Sent: Monday, October 01, 2001 12:46 PM
    > Subject: [noparse][[/noparse]basicstamps] Any way to send variables to a function/procedure
    >
    >
    > > While the gosub command is great for reusing code, it would be
    > > WONDERFUL to be able to send variables to them. Does anyone know if
    > > this can be done? Are variables always global? For example I'd like
    > > to do this:
    > >
    > > MAIN:
    > > variable1 ='code here to get a value from a pin
    > > variable2 = doNumberCrunching(variable1)
    > > goto Main
    > >
    > > doNumberCrunching(aNumber)
    > > aNumber = aNumber + 1
    > > doNumberCrunching = aNumber
    > >
    > >
    > > If you've programmed in C or vbscript, or regular basic, you know
    > > what I mean.
    > >
    > > I'm pretty sure subs can't take variables, but is there any kind of
    > > work around?
    > >
    > > Thanks!
    > >
    > >
    > > To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@yahoogroups.com
    > > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    > >
    > >
    > > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    > >
    > >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and Body
    of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >

    Sean T. Lamont, CTO / Chief NetNerd, Abstract Software, Inc. (ServNet)
    Seattle - Bellingham - Vancouver - Portland - Everett - Tacoma - Bremerton
    email: lamont@a... WWW: http://www.serv.net
    "...There's no moral, it's just a lot of stuff that happens". - H. Simpson
  • ArchiverArchiver Posts: 46,084
    edited 2001-10-02 13:48
    --- In basicstamps@y..., nospam@e... wrote:
    > While the gosub command is great for reusing code, it would be
    > WONDERFUL to be able to send variables to them. Does anyone know
    if
    > this can be done? Are variables always global? For example I'd
    like
    > to do this:
    >
    > MAIN:
    > variable1 ='code here to get a value from a pin
    > variable2 = doNumberCrunching(variable1)
    > goto Main
    >
    > doNumberCrunching(aNumber)
    > aNumber = aNumber + 1
    > doNumberCrunching = aNumber
    >
    >
    > If you've programmed in C or vbscript, or regular basic, you know
    > what I mean.
    >
    > I'm pretty sure subs can't take variables, but is there any kind of
    > work around?
    >
    > Thanks!

    To me, this is one of the big shortcomings of the Stamp BASIC
    language, but the reason for it is pretty obvious - there simply
    isn't enough memory to do it. You would need some space to store the
    parameters (this is generally done using a "stack" that stores the
    address of each parameter passed). With only a few bytes to work
    with, any kind of a stack would quickly exhaust the available memory.
    As I understand it, the Stamp uses only 4 bytes of memory for
    subroutine calls - these are used to stack a pointer to the return
    address from each call, which is actually stored in EEPROM. This
    saves memory, but is the reason subroutines may only be nested 4 deep.

    So yes, all variables are global. On the more advanced stamps, you
    could use the scratchpad RAM to implement a stack of your own, but
    you'd have to do all the management of it in your own code
    (placing/removing parameters, keeping the pointer,etc.) and you could
    still only call 4 levels deep. So instead of:

    CALL SUBR(A, B, C)

    you'd have to write: (pseudo-code)

    Move A to stack (somewhere in Scratchpad, pointed to by stack pointer)
    increment stack pointer
    move B to stack
    increment stack pointer
    move C to stack
    increment stack pointer
    GOSUB SUBR
    subtract 3 from stack pointer

    Your subroutine would then have to retrieve the data from the stack,
    using the stack pointer and negative offsets to find each piece of
    data. This is all a lot of work, but it could be done.

    If A, B, and C were different sizes (not single bytes), you'd have to
    change the moving, incrementing, and decrementing of the stack
    pointer accordingly.

    Most of the implementation of the Stamp is based on minimizing memory
    usage, and this affects the functionality available. This is just one
    of many examples. To implement a real subroutine function, you'd
    probably need 100 bytes or more of stack space.

    Chuck
  • ArchiverArchiver Posts: 46,084
    edited 2001-10-02 14:57
    Thanks all for your replies! Can't believe I didn't join this group before.

    -D
    Original Message
    From: "Chuck Davis" <cdavis@o...>
    To: <basicstamps@yahoogroups.com>
    Sent: Tuesday, October 02, 2001 6:48 AM
    Subject: [noparse][[/noparse]basicstamps] Re: Any way to send variables to a function/procedure


    > --- In basicstamps@y..., nospam@e... wrote:
    > > While the gosub command is great for reusing code, it would be
    > > WONDERFUL to be able to send variables to them. Does anyone know
    > if
    > > this can be done? Are variables always global? For example I'd
    > like
    > > to do this:
    > >
    > > MAIN:
    > > variable1 ='code here to get a value from a pin
    > > variable2 = doNumberCrunching(variable1)
    > > goto Main
    > >
    > > doNumberCrunching(aNumber)
    > > aNumber = aNumber + 1
    > > doNumberCrunching = aNumber
    > >
    > >
    > > If you've programmed in C or vbscript, or regular basic, you know
    > > what I mean.
    > >
    > > I'm pretty sure subs can't take variables, but is there any kind of
    > > work around?
    > >
    > > Thanks!
    >
    > To me, this is one of the big shortcomings of the Stamp BASIC
    > language, but the reason for it is pretty obvious - there simply
    > isn't enough memory to do it. You would need some space to store the
    > parameters (this is generally done using a "stack" that stores the
    > address of each parameter passed). With only a few bytes to work
    > with, any kind of a stack would quickly exhaust the available memory.
    > As I understand it, the Stamp uses only 4 bytes of memory for
    > subroutine calls - these are used to stack a pointer to the return
    > address from each call, which is actually stored in EEPROM. This
    > saves memory, but is the reason subroutines may only be nested 4 deep.
    >
    > So yes, all variables are global. On the more advanced stamps, you
    > could use the scratchpad RAM to implement a stack of your own, but
    > you'd have to do all the management of it in your own code
    > (placing/removing parameters, keeping the pointer,etc.) and you could
    > still only call 4 levels deep. So instead of:
    >
    > CALL SUBR(A, B, C)
    >
    > you'd have to write: (pseudo-code)
    >
    > Move A to stack (somewhere in Scratchpad, pointed to by stack pointer)
    > increment stack pointer
    > move B to stack
    > increment stack pointer
    > move C to stack
    > increment stack pointer
    > GOSUB SUBR
    > subtract 3 from stack pointer
    >
    > Your subroutine would then have to retrieve the data from the stack,
    > using the stack pointer and negative offsets to find each piece of
    > data. This is all a lot of work, but it could be done.
    >
    > If A, B, and C were different sizes (not single bytes), you'd have to
    > change the moving, incrementing, and decrementing of the stack
    > pointer accordingly.
    >
    > Most of the implementation of the Stamp is based on minimizing memory
    > usage, and this affects the functionality available. This is just one
    > of many examples. To implement a real subroutine function, you'd
    > probably need 100 bytes or more of stack space.
    >
    > Chuck
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >
Sign In or Register to comment.