Is it safe to return an array from a function?

Peter W A Wood peterwawood at gmail.com
Sun Sep 20 22:28:21 EDT 2015


I understand that arrays are passed by reference in LiveCode not by value. I assume that is also that case if you return an array from a function (i.e. the reference to the array is returned not its content). When I tried to return an array from a function, I found that not only you can do so but multiple calls of the function provide different arrays. As the array being returned from the function is local to the function, I’m guessing that LiveCode is creating a new local array for each function call and returning a reference to the local variable.

Here is the code that I ran under LiveCode Server:

function returnArray
  local a
  put 1 into a[1]
  return a
end returnArray

put returnArray() into b
put b[1] & return				—> prints 1

put returnArray() into c
put c[1] & return				—> prints 1	     	  

put 3 into c[1]

put b[1] & return				—> prints 1
put c[1] & return				—> prints 2

Is it safe to use such a technique or will the reference to the array become invalid at some stage?

Peter







More information about the use-livecode mailing list