Announcement

Collapse
No announcement yet.

passing array back from a function

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

  • passing array back from a function

    Hi

    I was thinking of re-writing part of my code, and before I do so
    thought I'd check to see if it's even gonna work..

    I'm passing an array to a function for processing. Is it possible
    to have a results-array passed back?

    ------------------
    Henning
    Henning

  • #2
    Arrays cannot be returned as a function result (FUNCTION xyz AS array() is not a valid prototype), but can be passed BYREF. Therefore, the solution is to pass the "result" array as a parameter.

    Something like this:
    Code:
    'pseudocode
    FUNCTION ProcessArray&(Arr1&(), Arr2&()) AS LONG
      REDIM Arr2&(100)
      IF ISFALSE ERR THEN 
        Arr2&(100) = Arr1&(1)
        FUNCTION = %TRUE ' return an success/failure code
      ELSE
        FUNCTION = %FALSE
      END IF
    END FUNCTION
     
    FUNCTION PBMAIN
      DIM A1&(100), A2&(0) ' A1 is data array, A2 is result array
      A1&(1) = 999&
      IF ProcessArray&(A1&(), A2&()) THEN
        x& = UBOUND(A2&())
        MSGBOX STR$(A2&(x&))
      ELSE
        MSGBOX "An error occurred!"
      END IF
    END FUNCTION

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

    Comment


    • #3
      Thank you Lance

      ------------------
      Henning
      Henning

      Comment

      Working...
      X