Methods with returns
rtarbell
Posts: 7
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,
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, , @StackAdd)
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,
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, , @StackAdd)
Comments
If you know other languages, just use
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The more I know, the more I know I don't know.· Is this what they call Wisdom?