Announcement

Collapse
No announcement yet.

Variables in function declarations

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

  • Variables in function declarations

    Hello all, I greatly appreciate these forums, I have learned a
    great deal.

    However, does anybody know how to declare a function so that it
    will accept more than one type of variable?

    I have written a function to create a hex string out of a number,
    (I wrote one instead of the PB function because I need it to
    return the leading zeros as appropriate.) Anyway, I would like
    to be able to just pass it ints, words, longs, ... without the
    bycopy statement? I guess it is mostly a personal irritation, but
    I am looking for ideas.

    Thanks guys,

    Paul Harness

    ------------------

  • #2
    You can use AS EXT. For example, the function below accepts passing any kind of numeric variable.

    Code:
    FUNCTION DS_FixedRound(BYVAL xNum AS EXT) AS CUX
       FUNCTION = ROUND(xNum + 0.005 * SGN(xNum), 2)
    END FUNCTION

    ------------------


    [This message has been edited by JOSE ROCA (edited December 13, 2002).]

    Comment


    • #3
      Thank you very much. I had tried BYCOPY in the declaration, but
      that didn't work. The BYVAL and the EXT is exactly what I was
      looking for.

      THanks,

      Paul


      ------------------

      Comment


      • #4
        EXT will encompass most numeric types just fine but, it's useful to know
        that functions don't accept generic types by default because there can be
        side-effects involved in morphing the parameter types and, it's grossly
        inefficient to have to change types on the fly. Otherwise, there would be
        no need to ask this kind of question.

        Possibly, you're used to dealing with a medium-late version of Visual Basic,
        or one of the dBase-style languages that pretend there's no such thing as a
        data type. That can be convenient, but you pay a heavy price for the works
        behind the scenes, and PowerBASIC doesn't hide that tax from you.

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

        Comment


        • #5
          Code:
          UNION AnyDataType
           L   AS LONG
           EX  AS EXT
           S   AS SINGLE
           I   AS INTEGER
           DW  AS DWORD
           ...
          END UNION
          
          
          FUNCTION AcceptMePlease (WhatType AS LONG, Z AS AnyDataType) AS LONG
          
            SELECT CASE WhatType
              CASE  %TYPE_LONG
                CALL Processlong(Z.L)
              CASE  %TYPE_SINGLE
                CALL ProcessSingle (Z.s)
              CASE %TYPE_INTEGER
                CALL ProcessInteger (Z.I)
              yadda yadda
             END SELECT
          END FUNCTION
          With CC/3 and Win/7, you can also pass a one-element array in lieu of a UNION and use the ARRAYATTR function to get the datatype. (hmm, come to think of it, PB/DOS's ArrayInfo& function will also tell you the datatype).

          MCM


          [This message has been edited by Michael Mattias (edited December 14, 2002).]
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Thank you all for your input. On this function, I am willing
            to pay the cost of run time conversion between data types.

            I have actually used PB for all of my programming experience,
            except some c++ at school and for windows.

            I ended up writing the function with the byval in the function
            declaration, and it works great. I had tried bycopy, but that is
            for the function call.

            Anyway, thanks again.

            PS, these forums are great. So many ideas and examples.

            ------------------

            Comment

            Working...
            X