Reference Passing

Reference passing allows variable to be modified by the function it's passed to.

Passing By Value

Let's look at the following example:


num is incremented by 1 in theincrementValue function. Since it's passed by value it remains 0 outside of the incrementValue function.

Passing By Reference

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.

Array Arguments

Unlike other languages, arrays are not passed by reference by default.


To modify the array & needs to be placed at line 8.