Shop OBEX P1 Docs P2 Docs Learn Events
Methods with returns — Parallax Forums

Methods with returns

rtarbellrtarbell Posts: 7
edited 2007-10-07 01:59 in Propeller 1
In the tutorial for the Propeller, the examples of writing code are given for a toggle function for lighting LEDs. To me, the methods included are kind of like void functions (they take inputs, but do not yield any output to the caller).

An example method from the Propeller Manual:

In the top object, a call to the toggle method:
Toggle(16, 3_000_000, 10)


The toggle method defined in a separate method:

PUB Toggle(Pin, Delay, Count)
dira[noparse][[/noparse]Pin]~~ 'Set I/O pin to output direction
repeat Count 'Repeat for Count iterations
!outa[noparse][[/noparse]Pin] ' Toggle I/O Pin
waitcnt(Delay + cnt) ' Wait for Delay cycles



==> What if I want to get a return from the function? For instance, if I had a simple math method, could I do something like this:
'A, B, and C are long variables
C:= Addition(A, B)
Would C have the result returned?
How could I code the same example with the method performed in a new cog? Would it be like this:

C:=cognew(Addition(A, B), @StackAdd)

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-10-06 20:12
    Generally "Yes", but not with COGNEW; COGNEW delivers the number of the cog your new code is running in..

    If you know other languages, just use
    RETURN somevalue
    


    But there exists also a somewhat more confusing technique.

    But all this does not apply to parallel processes in another COG.
    Starting up a COG is a quite expensive operation and not meant to be used for just calling a tiny routine.
    Generally you install a "Server" in a COG that loops, fishing for parameters in some parameter area and rerturning its results there.

    Post Edited (deSilva) : 10/6/2007 8:19:24 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-10-06 21:03
    What you wrote won't work as you expect it. You wrote "C := cognew(Addition(A,B),@StackAdd)". The call to Addition doesn't really return. The Spin interpreter loads a new copy of the Spin interpreter into a spare cog and starts it executing on the method Addition with the stack you supplied, StackAdd, initialized with the values of A and B. As soon as the Spin interpreter has started loading, COGNEW will return with the number of the cog being used (or -1 if there were no cogs available). That's what's stored into C. The COGNEW sets up the new stack with a dummy return address that, if a return is done, the cog is stopped (like: temp := COGID followed by COGSTOP(temp)). Anyway, at some time after the COGNEW returns (typically hundreds of microseconds), the new cog will have started and will get around to executing the call to Addition which may or may not do what you expect at that point.
  • Ken PetersonKen Peterson Posts: 806
    edited 2007-10-07 01:59
    The Graphics object is an example of an object that spawns a "server" as deSilva mentioned. Have a look at it to see how it's done.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
Sign In or Register to comment.