Announcement

Collapse
No announcement yet.

array parameter in sub call

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

  • array parameter in sub call

    Hello guys,

    I can't figure how can I transmit an array, by reference, to a sub
    I've wrote this code:
    >>>>>>>>>>>>>
    dim a(4) as INTEGER
    cls
    a(0)=1
    a(1)=2
    a(2)=3
    a(3)=4
    call s_makeSum(a)
    end

    sub s_makeSum(b)
    dim i as INTEGER, sum as INTEGER
    sum=0
    for i=0 to 3
    ? b(i)
    sum=sum+b(i)
    next i
    ? ">"+STR$(sum)+"<"
    end sub
    >>>>>>>>>>>>

    why b(0)=b(1)=b(2)=b(3)=0 ???

    what is wrong whit my code?

    thanks alot,
    alex

  • #2
    Alex,
    Code:
    call s_makeSum(a())
    and
    Code:
    sub s_makeSum(b())
    .

    why b(0)=b(1)=b(2)=b(3)=0 ???
    Because the array hadn't got passed to the sub. If you had $DIM ALL metastatement
    at the beginning of your code, any reference to an undeclared variable or array
    would have been immediately signaled to you.

    ------------------
    Davide Vecchi
    [email protected]

    Comment


    • #3
      Davide,

      It worked for me when I used this:

      sub s_makeSum(b() as integer)

      Thank you very very much,
      Alex

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

      Comment


      • #4
        Alex, right, i had forgot that part.

        Off topic, i see that - in the SUB - you DIM the local vars without using LOCAL:
        Code:
        dim i as INTEGER, sum as INTEGER
        If you aren't already, be aware that in PB/DOS, if you have a module-level global var
        with the same name of a local var (like your "i"), within the procedure the var "i" won't be
        the local one but the global one. You won't be having any local var called "i".
        To have both the global and the local one, you need to use LOCAL in the procedure,
        either as a clause in the DIM (DIM i AS LOCAL INTEGER) or as a statement
        (LOCAL i AS INTEGER).
        However, if you omit LOCAL and no global var exists with that name, the var
        will be local.
        IMO it's good practice to always use LOCAL when declaring vars within a procedure,
        just to be sure.

        ------------------
        Davide Vecchi
        [email protected]

        Comment

        Working...
        X