Try to create as many let variable as possible to conserve memory.
Arguments of functions are a let constant . Below code will give error.
func add(a: Int, b: Int) {
a = 10
let sum = a + b
print(sum)
}
add(a: 10, b: 5)
To update the value of instance scope by a function using inout
using this function we update the value of variable.
var number = 5 // instance scope
// inside of a funciton local scope
// funciton pass by reference
func addNumber(a: inout Int) {
a += 10
}
addNumber(a: &number)
print(number)
Swapping two numbers using inout in a function
var x = 5
var y = 10
func swap(x: inout Int, y: inout Int) {
let temp = x
x = y
y = temp
}
swap(x: &x, y: &y)
print("x = \(x) and y = \(y)")
Output:
x = 10 and y = 5