esgici
I will try to explain with examples.
1. Array passed by value:
Code: Select all
PROCEDURE DoTest()
LOCAL aArr1 := {1,2,3}
AddElement(aArr1) //aArr1 passed by value
MsgBox(aArr1) //{1,2,3,4}
ChangeArray(aArr1) //aArr1 passed by value
MsgBox(aArr1) //{1,2,3,4}
RETURN
PROCEDURE AddElement(aArr2)
aAdd(aArr2, 4)
RETURN
PROCEDURE ChangeArray(aArr2)
aArr2 := {7,8,9}
RETURN
aArr1 := {1,2,3}
Creates new array {1,2,3} and assigns to the variable aArr1 a reference to this array.
It means, that aArr1 does not contain any array.
aArr1 contains a pointer (address in memory) to the beginning of the array.
AddElement(aArr1)
PROCEDURE AddElement(aArr2)
aArr1 is passed by value, which means, that is passed copy of variable.
aArr1 and aArr2 are different variables, but contain the same value - pointer to the same array.
aAdd(aArr2, 4)
Adds one element to the array pointed by aArr2.
Changes the contents and length of the array, but the pointer to the array does not change.
aArr1 and aArr2 still contain the pointer to the same array.
aArr2 := {7,8,9}
Creates new array {7,8,9} and assigns to the variable aArr2 a reference to this array.
Now aArr1 and aArr2 contain the pointers to two different arrays.
Array pointed by aArr1 has not been changed.
----------------------------------
2. Array passed by reference:
Code: Select all
PROCEDURE DoTest()
LOCAL aArr1 := {1,2,3}
AddElement(@aArr1) //aArr1 passed by reference
MsgBox(aArr1) //{1,2,3,4}
ChangeArray(@aArr1) //aArr1 passed by reference
MsgBox(aArr1) //{7,8,9}
RETURN
PROCEDURE AddElement(aArr2)
aAdd(aArr2, 4)
RETURN
PROCEDURE ChangeArray(aArr2)
aArr2 := {7,8,9}
RETURN
AddElement(@aArr1)
PROCEDURE AddElement(aArr2)
aArr1 is passed by reference, which means, that is passed the pointer (address) to this variable.
So aArr1 and aArr2 are the same variables and contain the same value - pointer to the same array.
aArr2 := {7,8,9}
Creates new array {7,8,9} and assigns to the variable aArr2 a reference to this array.
Now aArr2 contains the pointer to {7,8,9}.
Because aArr1 and aArr2 are the same, aArr1 also contains the pointer to {7,8,9}.
In this way original array has been changed.