1

I am using CFWheels and I have my action and private function as such:

private function update_arr(arr)
{
    arr[1] = "val 1.1";
    arr[2] = "val 2";
}

public function t_test_1()
{
    title = "Test 1";
    arr = ArrayNew(1) ;
    arr[1] = "val 1";
    update_arr(arr);
}

I want to pass the arr variable by reference into the function "update_arr", so the arr values are updated to what it is assigned to inside the function. Right now if I cfdump arr

it only gives me:

1 : val 1

I want it to be:

1: val 1.1
2: val 2
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Saad A
  • 1,135
  • 2
  • 21
  • 46
  • Arrays are passed by value [Refer this link](http://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java) – Avi Feb 21 '16 at 03:56
  • So how come it doesn't update the values? – Saad A Feb 21 '16 at 03:56
  • Please check the link and view the answers.The answer with an example is very helpful. – Avi Feb 21 '16 at 04:05
  • 1
    @Avi - Not quite. Java arrays are a different beast than what CF calls an "array". A CF array is actually a `java.util.List` object. Now having said that, ColdFusion arrays ARE typically passed by value - yes. @SaadA - Typically, [CF arrays are passed "by value" - not by reference](http://stackoverflow.com/questions/7616020/coldfusion-component-pointers/7617372#7617372). That means the function works with a totally separate copy of the array, and any changes it makes have absolutely no impact on the original array. To preserve the changes, return and capture the array from the function. – Leigh Feb 21 '16 at 05:56
  • 1
    Also, if you do a search of the archives there are several other threads on "by value" vs "by reference". Voting to close as a duplicate. – Leigh Feb 21 '16 at 06:06

0 Answers0