passing parameters by reference

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
bobcoffee
Posts: 10
Joined: Tue Jan 15, 2013 7:24 am

passing parameters by reference

Post by bobcoffee » Tue Mar 05, 2013 3:25 pm

I think I understand passing parameters by reference but I have a question about passing arrays by reference. Is it possible to pass arrays by reference as in the code snippet below? I have been unable to get referenced parameters to work with arrays.

In the dictionary it says
"Pass a parameter reference when you want a handler to change a variable in the calling handler, or when you want a handler to return more than one value."
I want to handle more than one value, so I thought I would write a handler that passes arrays by reference but I can't seem to get it to work. Any idea?

Code: Select all

//handler call_1
initFader  sFadeValueA[1], sAvailabliltyFaderA[1]

//another call_2 from a different location in code
initFader  sFadeValueA[2], sAvailabliltyFaderA[2]

//reference passing doesn't seem to work with arrays
on initFader  @pFadeValueA, @pAvailabliltyFaderA
     put 0 into pFadeValueA
     put "available" into pAvailabliltyFaderA
end initFader
coffee

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: passing parameters by reference

Post by magice » Tue Mar 05, 2013 4:45 pm

you should be able to pass it with just the array name (without the bracketed numbers). Try this test:

Code: Select all

on mouseUp
   put 10 into tNum[1]
   put 10 into tNum[2]
   answer tNum[1], tNum[2] --first answer
   addNumbers tNum
answer tNum[1], tNum[2] --third answer
end mouseUp

on addNumbers @tNum
   add 1 to tNum[1]
   subtract 1 from tNum[2]
   answer tNum[1], tNum[2] --second answer
end addNumbers
you should get an answer dialog from the call script saying 10,10 showing that there is a value in the array. Then you should get an answer dialog from the handler saying 11,9 showing that the array was passed. You will then get a third answer dialog again saying 11,9 from the mouseUp script showing that the values were passed back. Now keep in mind that the array is still not global, so as soon as the mouseUp is done the values are gone.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: passing parameters by reference

Post by mwieder » Tue Mar 05, 2013 6:44 pm

Coffee-

Yes, you've run into one of the current limitations of arrays. You can pass a reference to the entire array, but not a reference to an element in the array.
Hopefully arrays will get some attention soon with the engine refactoring.

bobcoffee
Posts: 10
Joined: Tue Jan 15, 2013 7:24 am

Re: passing parameters by reference

Post by bobcoffee » Wed Mar 06, 2013 1:13 am

mwieder

Thanks. I was afraid of that.
Lets hope they add this to the engine refactoring

--coffee

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: passing parameters by reference

Post by mwieder » Wed Mar 06, 2013 3:09 am

I hope multidimensional arrays get to be first-class objects as well.
But meanwhile there are any number of ways to accomplish what you're trying to do.
Just not... er... in the way you want to do it...

Hmmmm.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: passing parameters by reference

Post by snm » Wed Mar 06, 2013 8:12 am

You can pass full array and change only it's part in called function. Passing the array by reference means only that this array is not copied to run in another handler.

Marek

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: passing parameters by reference

Post by mwieder » Wed Mar 06, 2013 8:07 pm

Marek-

Yes, you can, but if the arrays are of any significant size this can be noticeably slower than access by reference. Passing the array to a function means a copy is made for the function's parameter, the changes are made to the copy, the copy is returned by the function, and then the copy has to be copied again into the original.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: passing parameters by reference

Post by snm » Wed Mar 06, 2013 10:26 pm

I wanted to say that, but missed "by reference" after "You can pass full array".

Marek

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: passing parameters by reference

Post by mwieder » Wed Mar 06, 2013 11:29 pm

Ah :o

I still need 4 more chars before I can submit this.

ColDrum
Posts: 2
Joined: Mon Dec 02, 2013 7:53 pm

Re: passing parameters by reference

Post by ColDrum » Wed Mar 05, 2014 5:04 pm

Is this still an issue in LiveCode?

I have to pass a whole array by reference, rather than passing an individual element of it to a procedure.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: passing parameters by reference

Post by mwieder » Thu Mar 06, 2014 7:45 am

You can pass an individual array element to a procedure.
What you can not do is pass a reference to that element array and act on it in the array.
Not sure if this answers your question.... if you want to modify an array element, you have two choices:

pass the array element itself to a procedure, work on it, put the modified value back into the array
or
pass a reference to the array to the procedure, work on it there.

But if you just want to use an element of an array without modifying it in place, you can just pass it as an argument to the procedure.

Post Reply