Reference Passing

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

Passing By Value

Primitive types (numbers, strings etc...) are passed by value.

Let's look at the following example:

Output:

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

Passing By Reference

Arrays and dictionaries are always passed by reference.

Output:

Since arrays are passed by reference, arr values remain modified outside the modifyArray function.