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 keyword ref needs to be added before argument in both function definition and call:

Output:

Since num is passed by reference at line 10, it's value has incremented inside the main function too.

Array Arguments

Arrays are always passed by reference.

Output:

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