Reference passing allows variable to be modified by the function it's passed to.
Let's look at the following example:
Output:
num is incremented by 1 in theincrementValue function. Since it's passed by value it remains 0 outside of the incrementValue function.
num
incrementValue
0
To pass by reference, the symbol & needs to be added before argument's name:
&
Since num is passed by reference at line 3, it's value has incremented outside the incrementValue function too.
line 3
Unlike other languages, arrays are not passed by reference by default.
To modify the array & needs to be placed at line 8.
line 8