Shop OBEX P1 Docs P2 Docs Learn Events
Problem Returning an array from a method (resolved) — Parallax Forums

Problem Returning an array from a method (resolved)

Greg LaPollaGreg LaPolla Posts: 323
edited 2009-07-07 01:22 in Propeller 1
Here is the method

PUB Mget | R[noparse][[/noparse]10], X                                                        

  meter.Init(RC_PIN, HL_PIN, CAP_VAL, COR_FACTR)        
  output.tx(output#CLS)
  waitcnt(clkfreq + cnt)
  repeat until ina[noparse][[/noparse]24] == 1                                               
    R[noparse][[/noparse]0] := meter.Resistance / 1066                                  
    waitcnt(clkfreq/10 + cnt)                                             
    output.tx(output#HOME)
    output.Str(String("Steps = "))
    output.Str(num.decf(R[noparse][[/noparse]0],3))                                       
    output.tx(output#CLREOL)

  repeat X from 1 to R[noparse][[/noparse]0]
    output.tx(output#CLS)
    waitcnt(clkfreq + cnt)
    repeat until ina[noparse][[/noparse]24] == 1                                             
      R[noparse][[/noparse]X] := meter.Resistance / 9                                     
      waitcnt(clkfreq/10 + cnt)                                           
      output.tx(output#HOME)
      output.Str(String("Step "))
      output.Str(num.decf(X,1))
      output.Str(String(" = "))      
      output.Str(num.decf(R[noparse][[/noparse]X],3))                                     
      output.tx(output#CLREOL)

  Result := @R




calling this method as follows

myVals := Mget

myVals is declared as long myVals[noparse][[/noparse]10]

mVals[noparse][[/noparse]X] is always corrupted. Is this not the correct way to return an array ? or is it not possible at all ?

I have even tried accessing as long[noparse][[/noparse]myVals][noparse][[/noparse]X] that doesn't seem to work either.

Any Ideas or thoughts on this ?


Thanks

Greg

Post Edited (Greg LaPolla) : 7/7/2009 1:24:46 AM GMT

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-07-06 22:54
    R will have to be a VAR variable for this to work. Local variables reside in the stack and effectively disappear once the method that hosts them returns.

    -Phil
  • SamMishalSamMishal Posts: 468
    edited 2009-07-07 00:40
    The way you should do this is by declaring MyVals as an array in the Vars section of the top object
    e.g.
    Var
    ··············· Long MyVals[noparse][[/noparse]10]
    ·
    The MGet method should be declared like this
    Pub MGet(ArrayAddress)
    ··············· ‘some code
    ··············· ‘code to fill array elements (maybe in a loop)
    ··············· Long[noparse][[/noparse]ArrayAddress][noparse][[/noparse]i] := someexpression
    ·
    Then when you call the method use this
    ··············· MGet(@MyVals)


    By the way if you want the type could be Word or Byte.....

    Sam
    ·
  • Greg LaPollaGreg LaPolla Posts: 323
    edited 2009-07-07 01:22
    Phil and Sam,

    Thanks for the input the problem is resolved.
Sign In or Register to comment.