Nested Method Call Question
coryco2
Posts: 107
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:
Modified Code:
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
Thanks.
Andy
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.
xcoor := object.getMouseX