Understanding reference types
[comp1110-1U]
Understanding reference types
[comp1110-1U]Definition 3 combined with Definition 2 tells us that when we call a function like fun1 or fun2 in Example 1, the array data itself is not copied into the function's frame, but instead just the
reference is. When fun1 or fun2 are called, the variable arr in their frame has the same value as the variable arr in the frame of main — they both point
to the same array. This means that when we call fun1, the contents of the array in main are observably changed, as the array indexing arr[0] follows the same pointer that is in main.
When we call fun2, however, nothing changes in the function main, because we don't follow the pointers, but instead just change the value of the local variable arr to point to a new array, then immediately discard it.