Announcement

Collapse
No announcement yet.

Array as Parameters

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

  • Array as Parameters

    I want to write a function that has an array as a parameter.
    Starting with the working scalar example,

    FUNCTION P(b AS LONG) AS LONG:MSGBOX STR$(b):END FUNCTION
    FUNCTION PBMAIN() AS LONG
    LOCAL a AS LONG
    a=4:P(a)
    END FUNCTION

    here is one of the many examples I tried that did not work.

    FUNCTION P(b() AS LONG) AS LONG:MSGBOX STR$(b(4)):END FUNCTION
    FUNCTION PBMAIN() AS LONG
    DIM a(4) AS LONG:a(4)=-4:P(a())
    END FUNCTION

    There is no documentation on how to pass arrays to a function.
    The old DOS PB compiler did have it. Perhaps it is no
    longer permitted in the CC and DLL verions?

    Anyone have any recommendations?

    BTW it was a pleasant surprise to find out that MSGBOX worked
    as the PRINT statement in the DLL compiler when executing
    from a DOS prompt. It would be useful to include that info
    in the manual under PRINT (not supported, see MSGBOX; then
    include an explanation in the REMARKS section of MSGBOX).


  • #2
    Hi Joe

    passing arrays BYREF works fine - the only problem you've got is that you declare P() as a function and disregard its return value (you didn't specify one).

    This works:

    Code:
    FUNCTION P(b() AS LONG) AS LONG
        MSGBOX STR$(b(4))
    END FUNCTION
    
    FUNCTION PBMAIN() AS LONG
        DIM a(4) AS LONG
        a(4)=-4
        'Need to use CALL Syntax or simply P a()
        CALL P(a())
        P a()
    END FUNCTION
    Cheers

    Florent

    Comment


    • #3
      FUNCTION P() is expected to return something. Either make it a SUB or use
      "lRes& = P(a())" or even "CALL P(a())". The following works fine:

      Code:
      #COMPILE EXE
       
      FUNCTION P(b() AS LONG) AS LONG
        MSGBOX STR$(b(4))
      END FUNCTION
       
      FUNCTION PBMAIN() AS LONG
        DIM a(4) AS LONG
        a(4)=-4
        CALL P(a()) 'or lRes& = P(a())
      END FUNCTION

      Comment


      • #4
        Florent Heyworth and Borje Hagsten

        Fellows, thanks. It never occured to me there was a difference between
        P(a()) and CALL P(a()) [a.k.a. P a(), which I prefer).

        Had I looked at the CALL documentation I would have found an example of
        a call of a function with an array as a parameter. And looking further
        afield under the memeory layout for arrays's, the functions LBOUND
        and UBOUND which implied the existence of array parameters.

        Thanks for your help.

        Comment

        Working...
        X