|
Post by MrHiggins on Dec 18, 2021 11:00:11 GMT -5
Hi,
It seems passing an array value by ref does not change the array elements value after the subroutine has completed? Whilst I understand you can't pass a whole array to a subroutine, I would have expected to be able to pass an array element by ref like it was a normal variable.
Here is some example code demonstrating
print "This creates a value and then passes value by ref" val = 42 print "val is "; val call changeNumber val print "val is change to "; val print print "Now we will pass a single array value by ref" print dim numbers(0) numbers(0) = 42 print "numbers(0) is "; numbers(0) call changeNumber numbers(0) print "numbers(0) is is still? "; numbers(0)
sub changeNumber byref number print "The value passed to changeNumber is "; number number = 10 print "value in changeNumber has been changed to "; number end sub
|
|
|
Post by Rod on Dec 18, 2021 11:28:54 GMT -5
Yes, unfortunately not. We cannot pass whole arrays and while an element seems to pass byref certainly does not work. Its all to do with arrays being global by default. I think you would have similar problems passing a global variable. Since both arrays and global variables are visible within subs and functions Liberty expects they be changed directly in the sub or function. Bug? not qualified to say.
|
|
|
Post by mknarr on Dec 18, 2021 11:32:42 GMT -5
Try This.
dim numbers( 0) numbers(0) = 42 print "numbers(1) is "; numbers(0) call changeNumber numbers(0) print "numbers(1) is is still? "; numbers(0) end
sub changeNumber byref number print "The value passed to changeNumber is "; number number = 10 numbers(0)=number 'I put the new number in the array. You can use a variable instead of 0 print numbers(0) print "value in changeNumber has been changed to "; number end sub
|
|
|
Post by Walt Decker on Dec 18, 2021 13:31:56 GMT -5
Mr. Higgins, an alternate method:
' DIM Chars$(20)
Chars$(0) = "ABC"
Chars$(0) = FN.SetChar$(Chars$(0)) PRINT Chars$(0)
END
FUNCTION FN.SetChar$(A$)
PRINT A$ A$ = "DEF"
FN.SetChar$ = A$ END FUNCTION '
|
|
|
Post by MrHiggins on Dec 18, 2021 14:21:52 GMT -5
Thanks for the ideas guys. The solution I've gone for in my working version is based on mknarr where instead of passing the value by ref I pass the indices of the array items.
|
|