‘inout’ in Swift

This post was originally posted at https://agrawalsuneet.github.io/blogs/inout-in-swift/ and reposted on Medium on 1st Mar 2019.
class Employee {
var name : String
var age : Int
init(name: String, age: Int){
self.name = name
self.age = age
}
}
func changeEmployeeData(emp : Employee){
employee.name = "Suneet"
employee.age = 25
}
let employee = Employee(name: "Random", age : 10)print(employee.name) //Random
print(employee.age) //10
changeEmployeeData(emp : employee)print(employee.name) //Suneet
print(employee.age) //25
var variable: Int = 1func changeNumber (num: Int) {
num = 2
print(num)
}
changeNumber(num : variable)
var variable: Int = 1func changeNumber (num:inout Int) {
num = 2
print(num) // 2
}
changeNumber(num : &variable)

Please continue reading at https://agrawalsuneet.github.io/blogs/inout-in-swift/

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store