Array as param of a sub ?

TWELVE

Active Member
Licensed User
Hello @all,

is it possible to handover an array to a sub...?

If not, do i need to use a global defined array that i can access from within the sub or are there other ways to achieve that..?

regards,

TWELVE
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Arrays need to be declared globally so are available to all subs.

Regards,
RandomCoder
 

mjcoon

Well-Known Member
Licensed User
Arrays need to be declared globally so are available to all subs.

But that does mean that if you want the effect of having a Sub that operates on different arrays in your program (to do some statistical analysis, for instance) you have to copy the array to a "parameter array" that the Sub uses and then perhaps copy a modified array again afterwards. It's a bit clunky compared with having real array "ByRef" parameters!

Mike.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is currently not supported.
However it is possible to copy a reference to an array and then work with that reference:
B4X:
Sub Globals
 Dim array1(100) As Number
 Dim array2(100) As Number
 Dim tempArray(0) As Number 'empty array
End Sub

Sub App_Start
 ...
 tempArray() = array1() 'copy by reference
 DuplicateAllItemsByTwo 'will affect array1
 tempArray() = array2()
 DuplicateAllItemsByTwo 'will affect array2
End Sub

Sub DuplicateAllItemsByTwo
 for i = 0 to ArrayLen(tempArray()) - 1
   tempArray(i) = tempArray(i) * 2
 next
End Sub
 

agraham

Expert
Licensed User
Longtime User
There is no need to copy the array. As an array variable actually holds a reference to the array you can assign the required array to a temporary array that your Sub uses. Note that if your temporary array is used again without assigning a new array to it it will corrupt your original array. Redeclaring the temp array or assigning it a junk array will obviate that danger. Note that this is very efficient, only array references are being assigned, there is no copying going on. For version 6.90 the arrays must match in rank and type.

B4X:
ub Globals
   'Declare the global variables here.
   Dim Temp(0)   
   Dim A(0)
End Sub

Sub App_Start
   Dim A(10)
   Temp() = A()
   Doit
   Doit ' won't corrupt A()
   Msgbox(A(0))
   'Form1.Show
End Sub

Sub Doit
   Temp(0) = 10
   Dim Temp(1) ' for safety of the original array
End Sub
 

TWELVE

Active Member
Licensed User
That's way to complicated and inefficient.I can't see any advantage in referencing arrays to other arrays ( or even copy them) just to use them within a sub - in regards of what the sense of a sub is - for me it is a bit structure and encapsulation, which make some kind of procedural basic programming possible.In other words, i try to isolate the code within the sub from the mainpart ( or at least from the calling code piece).

I use a lot of nesting with the subs ( which i call procedures for myself - because this is what they are) to make the code more strict and clean - i do not want to share variables between main code and subs because of the very well known side effects of this practice.If i use the original array or just a copy or reference to it - i still use a global var and that's exactly what we wanna avoid whenever possible.

Have a look at PureBasic, which is my favourite procedural basic dialekt...


regards,

TWELVE
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…