When passing an array or placing the & (ampersand) before the the variable name will access the variable's address in memory and pass it to the function being called. Observe, instead of passing the value contained at the variable's address, the address itself is provided, and the function will receive the address where the variable in memory. Therefore, any changes made to the contents stored at the address will be seen by both the calling and called locations, allowing the function called to modify the same memory being referenced in the calling location.
Recall, when using an entire array, the reference to the first index position of the array is stored in the array name. Thus, passing an array passes the address of every index position in the array, provided the index offset range of 0 to N - 1 (the size of the array minus one) is referenced.
In order to utilize the & to access the address of an individual variable, we will need to review pointers first and will do so shortly.
Consider the example of sending and receiving a network path ( dropbox )to a file, instead of sending the file as an attachment. The receiver is given the path to where the file exists in memory, the file's address. Any changes made by the receiver to the file will modify the same memory location the sender is using to store the file.
The file pass_by_example includes two swap functions. The function swap_by_reference shows Passing By reference. Here is a review of Pass By Reference within swap_by_reference.
void swap_by_reference( int reference[], int index_1, int index_1 )
When a function is called any parameters passed to the method are Passed By Reference or by Value. Passing individual variables and literal values will utilize Pass By Value, when only the variable or value is listed. Only the literal value itself or the literal value stored within the variable will be given to the function called. The function will not receive a reference to where the variable passed exists in memory. Therefore, the function called will not be able to modify the variable of the calling location.
Consider the example of sending and receiving an email attachment. The receiver is given all of the data within the attachment, the value of the file received. Any changes made by the receiver to the file will only be reflected within the receiver's copy. The sender's copy will remain unchanged.
The file pass_by_example includes two swap functions. The function swap_by_value shows Passing By Value. Here is a review of Pass By Value within swap_by_value.
void swap_by_value( int value_1, int value_2 )