Shop OBEX P1 Docs P2 Docs Learn Events
Nested Method Call Question — Parallax Forums

Nested Method Call Question

coryco2coryco2 Posts: 107
edited 2012-05-26 07:18 in Propeller 1
I recently modified the Test Parallax Mouse.spin object to try out an idea for a nested method call, and found something I am wondering if anyone could explain to me. I moved the value assignment for the mousex and mousey variables (which I made into global variables) into a couple of seperate PUB methods, and then called them from the main MouseDisplay method, but they no longer return any values. Why not? I assumed the two versions would give the same result.

Original Test Parallax Mouse.spin code:
PUB MouseDisplay | mousex, mousey, mousez, mousebtns

  mouse.start(24, 25)                            ' Start mouse
  debug.start(31, 30, 0, 57600)                  ' Start serial connection

  waitcnt(clkfreq + cnt)                         ' Wait 1 s before starting

  ' Display static text.
  debug.str(String(CLS, "Mouse", CR, "x = ", CR, "y = ", CR, "scroll = ", CR, "CRL", HOME))

  repeat
    
    mousex += mouse.delta_x                      ' Get mouse data    
    mousey += mouse.delta_y
    mousez += mouse.delta_z
    mousebtns := mouse.buttons

    debug.str(string(CRSRXY, 4, 1))              ' Display mouse data    
    debug.dec(mousex)
    debug.str(string(CLREOL, CRSRXY, 4, 2))
    debug.dec(mousey)
    debug.str(string(CLREOL, CRSRXY, 9, 3))
    debug.dec(mousez)
    debug.str(string(CLREOL, CR, CR))
    debug.bin(mouse.buttons, 5)
    waitcnt(clkfreq/20 + cnt)

Modified Code:
VAR

  LONG  mousex
  LONG  mousey
  LONG  mousez
  LONG  mousebtns

PUB MouseDisplay

  mouse.start(24, 25)                            ' Start mouse
  debug.start(31, 30, 0, 57600)                  ' Start serial connection

  waitcnt(clkfreq + cnt)                         ' Wait 1 s before starting

  ' Display static text.
  debug.str(String(CLS, "Mouse", CR, "x = ", CR, "y = ", CR, "scroll = ", CR, "CRL", HOME))

  repeat
    
    mousex +=GetMouseX                      ' Get mouse data    
    mousey +=GetMouseY
    mousez += mouse.delta_z
    mousebtns := mouse.buttons

    debug.str(string(CRSRXY, 4, 1))              ' Display mouse data    
    debug.dec(mousex)
    debug.str(string(CLREOL, CRSRXY, 4, 2))
    debug.dec(mousey)
    debug.str(string(CLREOL, CRSRXY, 9, 3))
    debug.dec(mousez)
    debug.str(string(CLREOL, CR, CR))
    debug.bin(mouse.buttons, 5)
    waitcnt(clkfreq/20 + cnt)
    
PUB GetMouseX
    mouse.delta_x

PUB GetMouseY
   mouse.delta_y

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2012-05-25 17:26
    Your methods return nothing, try this:
    PUB GetMouseX
      [COLOR="red"]return[/COLOR] mouse.delta_x
    
  • coryco2coryco2 Posts: 107
    edited 2012-05-25 17:42
    Ah...ok. The Prop Manual says on page 196 that "every method has an implied RETURN at its end" which I took to mean that the result of whatever code was in the called method would be returned. But I see now that unless the called method code assigns Result some value, it will return zero. Fair enough.

    Thanks.
  • AribaAriba Posts: 2,690
    edited 2012-05-25 18:32
    coryco2 wrote: »
    Ah...ok. The Prop Manual says on page 196 that "every method has an implied RETURN at its end" which I took to mean that the result of whatever code was in the called method would be returned. But I see now that unless the called method code assigns Result some value, it will return zero. Fair enough.

    Thanks.
    To make use of this implied return, you need to specify a name for the returned variable and assign a value:
    PUB GetMouseX : dx
        dx := mouse.delta_x
    

    Andy
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-26 07:14
    And just to add one more way to do this:
    PUB GetMouseX
      [B]result[/B] := mouse.delta_x
    

    result is the default name of the return variable.

    result is the only local variable initialized to zero. You're original code would have returned zero since result hadn't been changed from its initialized state.
  • turbosupraturbosupra Posts: 1,088
    edited 2012-05-26 07:18
    ^ This is how I do this as well, result makes things pretty easy IMO and when I call getMouseX, I would say

    xcoor := object.getMouseX
Sign In or Register to comment.