passing arrays by name to a sub

Recurrent H/W and software problems
Post Reply
Stuart
Posts: 136
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

passing arrays by name to a sub

Post by Stuart »

Passing arrays is potentially very useful. Basic with subs and parameters is a wonderful thing. But I'm having trouble getting the format right.
This test program illustrates the point. How should I code this simple task?

Code: [Local Link Removed for Guests]

dim my_array(20) = 1,2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20
dim my_other_array(20)
adim = 20

copy_array my_array(),my_other_array(),adim
'with braces, the error is "Type mismatch line 5"
'without braces the error is "No such variable line 5"
'if I put a value inside the braces the error is "Type mismatch line 5"

for p=1 to adim
wlog my_other_array(p)
next
end

sub copy_array(in_array,out_array,array_dim)
dim in_array(array_dim)
dim out_array(array_dim)
for i= 0 to dim
   out_array(i) = in_array(i)
next
end sub
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 47 times
Been thanked: 50 times

Re: passing arrays by name to a sub

Post by bugs »

There was an error in one of the earlier versions - what version are you using?
Stuart
Posts: 136
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

Re: passing arrays by name to a sub

Post by Stuart »

1.51.5 currently
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 47 times
Been thanked: 50 times

Re: passing arrays by name to a sub

Post by bugs »

OK - that took a while to find!
I have marked the changes with comments - hope it helps. (using ver 1.52.1 but don't think it matters)

Code: [Local Link Removed for Guests]

' passing arrays

dim my_array(20) = 1,2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20
dim my_other_array(20)
adim = 20

'* copy_array my_array(),my_other_array(),adim
copy_array my_array(), my_other_array(),adim      'this looks like a bug - must have a space after the first comma or get syntax error

for p=0 to adim-1                                'note:- arrays are 0 to n unless use OPTION.BASE 1
  wlog my_other_array(p)
next
end

'* sub copy_array(in_array,out_array,array_dim)
sub copy_array(in_array(),out_array(),array_dim)  ' added braces
  dim in_array(array_dim)
  dim out_array(array_dim)
'*  for i= 0 to dim
  for i= 0 to array_dim                            'changed reserved word DIM to array_dim
     out_array(i) = in_array(i)
  next
end sub
Stuart
Posts: 136
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

Re: passing arrays by name to a sub

Post by Stuart »

Thanks for looking for it, I'll give that a try.

As I didn't get beyond line 5 I hadn't found my own bugs!
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 47 times
Been thanked: 50 times

Re: passing arrays by name to a sub

Post by bugs »

Hi Stuart,

Understood - no criticism intended - just noted as I went along. :)
I retyped, copied, pasted, retyped umpteen times before the real bug surrendered. Reported in the sotware bugs forum area.
Stuart
Posts: 136
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

Re: passing arrays by name to a sub

Post by Stuart »

It seems that there needs to be one and only one space after the first comma in the example above, at least in the code that I am using.
Post Reply