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:

Output:

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:

Output:

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.

Output:

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