Shop OBEX P1 Docs P2 Docs Learn Events
Passing variable parameters to and from a method. — Parallax Forums

Passing variable parameters to and from a method.

RichardFRichardF Posts: 168
edited 2007-06-02 23:00 in Propeller 1
Just getting started with the Spin language. I am studying the manual and looking at code examples on this forum. What is eluding me is how to pass a variable parameter value back to the calling object or method after it has been changed by the called method. In Pascal, C and QBasic you could send a variable to a procedure by reference (the address where the variable value resided, also use pointers) and then the calling program had access to any changes made by the called procedure. It appears to me that with Spin I can only pass a value·argument·to a method and therefore·lose all ability to see any changes in the passed variable after the called method changes it. What am I missing here?

Example:
Long· stack0[noparse][[/noparse]10]
Byte· temperature

Pub main
· Cognew(Outside_temperature(temperature), stack0)
· dira[noparse][[/noparse]16..23]~~
· repeat·
··· outa[noparse][[/noparse]16..23] := temperature
··· ... do something else...

Pri Outside_temperature(temp)
· temp := ...a process to read a temp probe

I want true parallel processing, e.g.·main can read·outside temperature whenever·it wants·to.

Thanks,
Richard

P.S. We all know I am missing something simple here, so let it rip; I have thick skin tongue.gif .

Comments

  • KaioKaio Posts: 253
    edited 2007-06-02 23:00
    You are right, Spin can only pass parameters by value. But a value could be a pointer of your variable.

    Example:
    Long  stack0[noparse][[/noparse]10]
    Byte  temperature
     
    Pub main
      Cognew(Outside_temperature([b]@[/b]temperature), stack0)
      dira[noparse][[/noparse]16..23]~~
      repeat 
        outa[noparse][[/noparse]16..23] := temperature
        ... do something else...
     
    Pri Outside_temperature(temp)
      BYTE[noparse][[/noparse]temp] := ...a process to read a temp probe
    
    



    On Cognew you pass the address of the variable temperature to the method, where you have to use the keyword BYTE to access the address as byte. There exist also keywords for WORD and LONG to access the memory at this size.
Sign In or Register to comment.