Shop OBEX P1 Docs P2 Docs Learn Events
When to use global variables vs using method RETURN variables — Parallax Forums

When to use global variables vs using method RETURN variables

homosapienhomosapien Posts: 147
edited 2011-04-28 11:02 in Propeller 1
I am slowly working my way into the SPIN language and was wondering if there is any accepted good programming practice of when to use a method RETURN variable vs just declaring the variable as a global.

I was writing a simple object to monitor battery voltage (prop system supply) using a RC circuit and RC time counter. It became apparent that this simple object (as practically any programming task) could be written either way:

Using Global Vars:
CON
  _clkmode = xtal1 '+ pll16x        'Using no PLL so I can use damaged PPB                       
  _xinfreq = 5_000_000

  'PIN CONNECTIONS
  debugLED      = 0   'FOR TESTING

  Vdd         = 14      'voltage to monitor


  'calculated constants for calculating Vx
  c1    = 26615
  c2    = 18
  
VAR

  long  rct
  long  Vx

  
OBJ

  PST:  "Parallax Serial Terminal"      'FOR TESTING


PUB MAIN

     'init
    PST.start(9600)        'TEST 
    'dira[debugLED] := 1     'TEST

    repeat
      getVoltage(vdd)
      
      PST.dec(Vx)
      PST.newline
      
      waitcnt(clkfreq + cnt)
      !outa[debugLED]


      
PUB getVoltage(inPin)  

    getRCT(inPin)
    Vx := c1/rct*10+(c1//rct*10/rct) + (10 * c2)

    
      
PUB getRCT(inPin) 

  'init
    outa[inPin] := 0    'low
    dira[inPin] := 1    'output
    rct := 0  'zero counter

    waitcnt(clkfreq + cnt)      'pause 1 sec to drain cap
    dira[inPin] := 0    'input
    
    repeat until ina[inPin] <> 0
        rct++

Using Return Values:
CON
  _clkmode = xtal1 '+ pll16x        'Using no PLL so I can use damaged PPB                       
  _xinfreq = 5_000_000

  'PIN CONNECTIONS
  debugLED      = 0   'FOR TESTING

  Vdd         = 14      'voltage to monitor


  'calculated constants for calculating Vx
  c1    = 26615
  c2    = 18

  
OBJ

  PST:  "Parallax Serial Terminal"      'FOR TESTING


PUB MAIN

     'init
    PST.start(9600)        'TEST 
    'dira[debugLED] := 1     'TEST

    repeat      
      PST.dec(getVoltage(vdd))
      PST.newline
      
      waitcnt(clkfreq + cnt)
      '!outa[debugLED]


      
PUB getVoltage(inPin) : Vx  |  rct

    rct := getRCT(inPin)
    Vx := c1/rct*10+(c1//rct*10/rct) + (10 * c2)

    
      
PUB getRCT(inPin)  : rct

  'init
    outa[inPin] := 0    'low
    dira[inPin] := 1    'output
    rct := 0  'zero counter

    waitcnt(clkfreq + cnt)      'pause 1 sec to drain cap
    dira[inPin] := 0    'input
    
    repeat until ina[inPin] <> 0
        rct++


I think the global var procedure is a little easier to read and understand, but I am wondering if there is a better chance of problems due to the fact that any method could conceivably access the vars meant for use by the voltage measuring object? Less global variables the better?

Any opinions/ anecdotal good/bad knowledge/stories?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-28 10:52
    Either solution works. When you have several pairs of methods / variables, it starts to get complicated and I'd stick with using returned values. If methods get moved into another object for some reason, you really need to use returned values since the global variables are not accessible outside the object. You could pass the address of a global variable in or out of the object, but that, again, makes things more complicated.
  • homosapienhomosapien Posts: 147
    edited 2011-04-28 11:02
    Hi Mike,

    I just realized that global vars are not accessible from other objects, so I think the RETURN variable is the way to go as to exploit the power of spin (ie build lots of useful objects that can be easily thrown together to make a quick solution to the task at hand....)
Sign In or Register to comment.