Announcement

Collapse
No announcement yet.

Declaring Arrays as Function Parameters

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

  • Declaring Arrays as Function Parameters

    Can someone please clarify the purpose of MIN and MAX
    when declaring array parameters in a Function? I've used VB and
    QB extensively in the past and the use of MIN and MAX seems a
    bit alien to me.

    Is its use optional? For instance, if I have an array declared
    as below:

    DIM MyList(1 TO 30) AS INTEGER

    Can I define my function as follows:

    DECLARE FindAverage (TheList())
    .
    .
    FUNCTION FindAverage TheList() AS INTEGER

    or must I define the function as:

    FUNCTION FindAverage TheList(MIN, 1) AS INTEGER




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

  • #2
    Generally this strategy is used in PBU code, rather than in the main code. Additionally, the MIN/MAX keywords are used in the DIM statement, not the sub/function prototype.

    Anyway, here is a brief example of the usage. See page 92 in the Refernce Guide for a description of the differences between MIN and MAX in a DIM statement.
    Code:
    DIM a(100:200) AS SHARED LONG
    CALL mysub
    PRINT a(100), a(200)
    END
     
    SUB MySub
     DIM a(MIN,1) AS SHARED LONG
     PRINT LBOUND(a(1)), UBOUND(a(1))
     a(100) = 1
     a(200) = 2
    END SUB
    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Thanks for your response. The use of MIN|MAX in a DIM statement
      makes sense. What is muddying the water a bit is the way that
      the Reference Guide shows that array parameters for functions
      must be declared using MIN|MAX (page 128). Theoretically,
      several difference arrays may be passed to the same function in
      various places in a program and these arrays don't always have
      to have the same upper and lower bounds. Having to specify in
      the function prototype if the array's lower bound is zero could
      present problems.

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

      Comment


      • #4
        Using the internal function ArrayInfo&, you can pass ANY type of array to a procedure, get its type and your procedure can act accordingly.

        You have to call the procedure passing the address of the array descriptor, as in...
        Code:
         DIM pDescriptor AS DWORD
        
          pDescriptor = VARPTR32 (array())
        
        FUNCTION PlayWithArray (BYVAL dAddr AS DWORD)
        
        ' use ArrayInfo& here to get the type of data in the array.
        
           
        END FUNCTION
        ArrayInfo& is documented in the PB/DOS help file under 'internal functions'.

        (It is also not available in PB/Windows, a real shame)

        If you need a more complete sample, let me know. I have some code I will send you or if I'm in a good mood I'll post it in the source code forum.


        ------------------
        Michael Mattias
        Racine WI USA
        [email protected]
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment

        Working...
        X