Shop OBEX P1 Docs P2 Docs Learn Events
Same code in more than one cogs — Parallax Forums

Same code in more than one cogs

EMHmark7EMHmark7 Posts: 93
edited 2014-05-15 22:15 in Propeller 1
Hi,

In SPIN on Propeller.

If I use a function in several cogs and I declare variables in it of same name in all instances (because same source code),
will it create separate instances of those variables?

I suppose yes, but wanted to make sure.

In fact, I want to copy paste codes that looks alike in 2 different functions but with same local variable names.

Thanks,
Marc

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-05-15 18:53
    EMHmark7 wrote: »
    If I use a function in several cogs and I declare variables in it of same name in all instances (because same source code), will it create separate instances of those variables?
    If you're referring to local variables (the ones after |) then yes, you get separate ones for each call/instance. Remember that they remain uninitialised (not zero'd, except result).
  • ElectrodudeElectrodude Posts: 1,658
    edited 2014-05-15 20:37
    You don't get separate global variables for each instance, but you do get separate locals, as Heater said. So you can cognew the same function twice. Make sure each cog gets its own stack space, though.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-05-15 22:15
    As Marko pointed out, when you launch a Spin method into its own cog, you do in fact get separate copies of the local variables for that method -- these will live in the stack space that you declare for the cog. Also, as pointed out, these variables are not initialized and you don't have access to them outside the method.
Sign In or Register to comment.