Announcement

Collapse
No announcement yet.

BYVAL BYREF BYCOPY

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • BYVAL BYREF BYCOPY

    Im trying to make a function that can handle LONGs and SINGLEs as inputs.

    I am confused about my declarations:
    FUNCTION MaxList( Var1 AS SINGLE, Var2 AS SINGLE ) AS SINGLE
    blah
    Blah
    blah
    END FUNCTION

    even tho I have declared Var1 and Var2 as SINGLEs I want to be able to call the function with LONGs ie

    Global i AS LONG, J AS LONG, x AS SINGLE
    x = Maxlist( i, j )

    is this BYVAL or BYCOPY?


    ------------------
    Kind Regards
    Mike

  • #2
    When you pass a LONG value as parameter to sub/function that expects a BYREF SINGLE, you'll have to use BYCOPY - the compiler will then do an implicit type conversion and passes the result BYCOPY .

    That is, it has to be evaluated as an expression, which BYCOPY will effectively do for you.

    You can force this two ways, thus:
    Code:
    FUNCTION MaxList( Var1 AS SINGLE, Var2 AS SINGLE ) AS SINGLE
        FUNCTION = var1 + var2
    END FUNCTION
     
    FUNCTION PBMAIN
        DIM a AS LONG, b AS LONG, Result!
        a = 10 : b = 20
        Result! = MaxList(BYCOPY a,BYCOPY b)
        Result! = MaxList((a),(b))
    END FUNCTION
    If you want the value back from the function (because the function is expecting BYREF parameters), you'll have to pass a SINGLE so it gets passed BYREF instead of BYCOPY.

    You can override the type checking by using BYVAL VARPTR(lngvar&) where lngvar& is a memory variable, but since the function expects the 32-bit value stored in the 32-bits occupied by lngvar& to actually be a SINGLE value, the results will likely be unpredictable, since the internal format of a LONG variable and the internal format of a SINGLE are quite different.

    If you try to use BYVAL VARPTR(lngVal&), you'll get unpredictable results at best. If you use BYVAL lngval& you almost certainly get a GPF.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Thx Lance,

      That makes it clear.

      How does a conversion for a SINGLE to a LONG take place internally? Is it truncation? or the same as INT()


      ------------------
      Kind Regards
      Mike

      Comment


      • #4
        It uses CSNG().

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Err, CLNG.

          ------------------
          Tom Hanlin
          PowerBASIC Staff

          Comment


          • #6
            Oops, good catch! CSNG() to produce a Single, CLNG() to produce a Long.

            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment

            Working...
            X