Call by reference vs call by value using Swift (Pre Swift 3)

If you newly started programming and do not have that much experience or exposure to pointers, addresses and all that memory stuff, you might be confused with call by reference (occurs with classes) and call by value (occurs with structs). If you already know what a function call is then hopefully this will help you a little bit to make the distinction more clear in your mind.

Below is a basic drawing that shows the memory and value difference. The physical memory address is where data is held. The value is what is held in that address. It is important to understand this difference.

Physical memory address vs value representation

When calling a function that takes a parameter (or more) it can take either a value (Integer, Double etc.) or reference (an instance of an object). We will start with calling a function by reference:

Calling a function by reference

When you have an instance of an object, and assign it to a variable that variable actually holds the physical address of that instance. So when you pass it as an argument to different functions left and right, only its address is passed around which is more efficient than moving the entire object as a variable holding just the address information holds much less memory space than the object itself (4 bytes for 32 but systems and 8 bytes for 64 but systems).

When the physical address of a variable is passed as a parameter to a function when the function is called, it is said that the function is called by reference. What happens is; since only the address of the variable is passed, any change made is made to the variable in that address, which is the same variable that was used when the function was called! Because of that, any change that is made to the variable will change the variable itself! Below is a piece of code and printout of the variables and the addresses of related variables:

Calling a function by reference

As you see, when we call the function callMeByReference by reference, passing the address of someNumber, and change the value in that address inside of that function, it changes the value of someNumber, as it is the variable living in that address! When we print out someNumber’s value after we called the function, its value has been changed! (Even though the function does not return any value)

Calling a function by value

When you call a function by value though, the parameter(s) is copied (cloned) in a different address of the memory. Even if you gave the parameter the same name as the variable you used when you called your function, it will be a different variable (with a local scope, meaning it will only be living inside of the function body when it is being executed)

To prove our point, please see the code and the output example below*:

Calling a function by value

So, here we go! Our variable anotherNumber has the value of 3 before we pass it to the function callMeByReference and its address 0x0000000112097750. When the function had the parameter anothermethod, its value was 3 also, but its address was different! Because it is not the same anotherNumber, it is just a clone (surrogate — hey Bruce Willis fans!!!)!

Because of that, even if we change the value of the parameter anotherNumber, it will not change the value of the variable anotherNumber that is outside the function. We print out both the value and address of the anotherNumber variable after the function is called, neither of them has changed, proving our point!

I hope this helps someone!

*Please not that “var” parameters are being deprecated in swift 3. They will have to be a constant (“let”).