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 in the main 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 5, it's value has incremented inside the main function too.

Array Arguments

Arrays are always passed by reference.

Output:

Though not specified via the & symbol, array still gets modified by the function it's passed to.