A concept not simple to understand in every programming language (mainly for the “new” developer) is to realize the difference between “Pass by reference and Pass by value”.
Pass by value means: “copy a value from a location of memory to another location of the memory.” Instead, pass by reference mean that we don’t copy a value in the destination but the reference (address) to the source.
I like to use these images to explain the differences:
Thus, when we pass by value is like copying a cell value from one cell to another, instead, when passing by reference, we set in the destination cell the address of the source (e5 in the example).
Let’s see with swift:
struct BoxStruct { var width = 0 var height = 0 } var littleBox = BoxStruct(width: 2, height: 3) var bigBox = BoxStruct(width: 57, height: 84) littleBox = bigBox print("Little box: width \(littleBox.width) height \(littleBox.height)") print("Bix box: width \(bigBox.width) height \(bigBox.height)") bigBox.width = 150 bigBox.height = 333 print("Little box: width \(littleBox.width) height \(littleBox.height)") print("Big box: width \(bigBox.width) height \(bigBox.height)")
Executing this code have:
Little box: width 57 height 84 Big box: width 57 height 84 Little box: width 57 height 84 Big box: width 150 height 333
In this case, we have a copy by value. We copy the content from bigBox to littleBox, thus both the boxes have the same value; after we change the value of bigBox and then the two boxes will contain different values.
Let’s see what happen with the class:
class TriangleClass { var width: Int var height: Int init(width: Int, height: Int) { self.width = width self.height = height } } var littleTriangle = TriangleClass(width: 2, height: 3) var bigTriangle = TriangleClass(width: 57, height: 84) littleTriangle = bigTriangle print("Little Triangle: width \(littleTriangle.width) height \(littleTriangle.height)") print("Big Triangle: width \(bigTriangle.width) height \(bigTriangle.height)") bigTriangle.width = 150 bigTriangle.height = 333 print("Little Triangle: width \(littleTriangle.width) height \(littleTriangle.height)") print("Big Triangle: width \(bigTriangle.width) height \(bigTriangle.height)")
Now we have this output:
Little Triangle: width 57 height 84 Big Triangle: width 57 height 84 Little Triangle: width 150 height 333 Big Triangle: width 150 height 333
This time is different! After the copy, the triangles have the same value; after we change the dimensions of the bigTriangle but happen a magic thing, the dimensions of the littleTriangle are changed! Why? For the class, the copy is done by reference, so writing “littleTrianlge = bigTriangle”, we are not copying the content of the big in the little, we are copying the reference like in the sheet example. Now every change in the big will be visible also in the little.
The same happen in the function, if we want modify the original value of the parameter passed to a function, these have be passed by reference, in this way:
func swap(a : inout Int, b: inout Int) { let dum = a a = b b = dum } var first = 2 var second = 5 swap(a:&first, b:&second) // now first = 5 and second = 2
For exercise try to remove the keyword “inout” and see what happen.