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

Func Call Question

RS_JimRS_Jim Posts: 1,768
edited 2009-11-19 02:22 in General Discussion
I cant seem to find in the help file how to pass a parameter to a Func.

for example I have a Func called Acc_error and I want to pass in __param3 the base number with which to calculate the error.· I will then return the result in __wparam12.· If I do this:
__param3 = AccBase
TmpW1 = Acc_Error
I get the correct results, however I thought if you declaired in the Func Def
Acc_Error Func 2,1
It would let you write
tmpW1 = AccError· AccBase
and it would pass accBase in __param1
where am I tripping up?




Post Edited (Radio Shack Jim) : 11/19/2009 2:35:06 AM GMT

Comments

  • ZootZoot Posts: 2,227
    edited 2009-11-19 02:14
    Because the second number in the function declaration is the number of parameters expected to be passed: 1.

    MyFunc FUNC return bytes, minimum number of parameters, maximum number of parameters

    If you are expecting to pass different numbers of bytes (sometimes a word, sometimes a word and a byte, etc), then you either need to parse and decide what to do based on __PARAMCNT (in your function itself):

    MyFunc FUNC 2, 1, 3

    FUNC MyFunc
    IF __PARAMCNT = 1 THEN ' only one byte? clear MSB of wparam12 and clear param3
    __PARAM2 = 0
    __PARAM3 = 0
    ELSEIF __PARAMCNT = 2 THEN
    __PARAM3 = 0
    ENDIF
    tmpW1 = __WPARAM12
    tmpB3 = __PARAM3
    ' do stuff
    RETURN tmpW1
    ENDFUNC




    --- OR ----

    you need to force the function call to "promote" values to Words, even if you pass a byte, e.g.

    MyFunc FUNC 2, 3, 3, Word, Byte

    would be a function that returns two bytes (__WPARAM12) and expects 3 bytes as a Word and a Byte. The cool thing is even if your value for the Word parameter is <256, SX/B will clear the upper byte of the parameter word. So....

    FUNC MyFunc

    tmpW1 = __WPARAM12
    tmpB3 = __PARAM3
    ' do some stuff
    RETURN tmpW1

    ENDFUNC

    If I do this:

    someWord = MyFunc, 1, 10

    then param1 = 1
    param2 = 0
    param3 = 10


    If I do this:

    someWord = MyFunc, 2000, 10

    then param1 = 2000 & $FF
    param2 = 2000 >> 8
    param3 = 10

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • RS_JimRS_Jim Posts: 1,768
    edited 2009-11-19 02:22
    Got it Zoot, Thanks
    RS_JIM
Sign In or Register to comment.