Can I pass an Array as a sub Parameter?

General FreeBASIC programming questions.
leopardpm
Posts: 1787
Joined: Feb 28, 2009 20:58

Can I pass an Array as a sub Parameter?

Postby leopardpm » Dec 28, 2018 2:54

More specifically, can I pass a UDT array, with possible arrays within it, as a parameter to a Subroutine?

Code: Select all

type wsp
    as ushort ws_pntr
    as ushort val
end type

type act
    as ushort index
    as string name
    as ushort TimeCost
    as ushort EffortCost
    as ushort NumOfReqWS
    as wsp ReqWS(3)
    as ushort NumOfEffWS
    as wsp EffWS(3)
    as string LocReqName
    as ushort LocReqPntr
end type

dim as act Actions(20)

sub test(byref array() as act)
' do stuff to the arry passed in here

end sub

    test(Actions())
   
    sleep
    end
dodicat
Posts: 5549
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Can I pass an Array as a sub Parameter?

Postby dodicat » Dec 28, 2018 3:24

Most definitely, but omit the byref before array() as act, otherwise you get a compile error.

Code: Select all

type wsp
    as ushort ws_pntr
    as ushort val
end type

type act
    as ushort index
    as string name
    as ushort TimeCost
    as ushort EffortCost
    as ushort NumOfReqWS
    as wsp ReqWS(3)
    as ushort NumOfEffWS
    as wsp EffWS(3)
    as string LocReqName
    as ushort LocReqPntr
end type

dim as act Actions(20)

sub test( array() as act)
    print ubound(array)
    print ubound(array(ubound(array)).effws)
    array(0).reqws(0).val=13
' do stuff to the array passed in here

end sub

    test(Actions())
    print actions(0).reqws(0).val
   
    sleep
    end
leopardpm
Posts: 1787
Joined: Feb 28, 2009 20:58

Re: Can I pass an Array as a sub Parameter?

Postby leopardpm » Dec 28, 2018 3:41

ah ha! Where in any documentation does it specify that the byref needs to not be included? I was going crazy... I mean, obviously an array can't really be passed 'byVal' as duplicating an entire Array could be memory blowing, etc... but....

in any case, Thank you! Problem solved
sancho3
Posts: 352
Joined: Sep 30, 2017 3:22

Re: Can I pass an Array as a sub Parameter?

Postby sancho3 » Dec 28, 2018 4:17

Note that array parameters are always ByRef and the ByRef keyword is neither required nor allowed for array parameters. When calling a function with an array argument the parenthesis must be supplied there too; see the examples.

From here.
leopardpm
Posts: 1787
Joined: Feb 28, 2009 20:58

Re: Can I pass an Array as a sub Parameter?

Postby leopardpm » Dec 28, 2018 4:37

thank you Sancho... my reading skills must be lacking....
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: Can I pass an Array as a sub Parameter?

Postby D.J.Peters » Dec 28, 2018 6:49

BYREF is not allowed I don't like it.

BYREF should be optional same as for strings and if we use the warning level -w pedantic it should be mandatory.

Joshy
Last edited by D.J.Peters on Dec 28, 2018 7:22, edited 1 time in total.
fxm
Posts: 8691
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Can I pass an Array as a sub Parameter?

Postby fxm » Dec 28, 2018 7:13

I think that could be done it at the same time as adding the array copy capability and the passing of arrays by value, as for the strings (by passing by reference a copy of the array).

Return to “General”

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests