iOS/Swift문법
[Swift 문법] 매개변수 inout - day9
안드뽀개기
2022. 3. 17. 13:33
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 매개변수 inout | |
// 매개변수 inout은 매개변수의 값을 변경하는 것을 의미 | |
let jobTitle = "개발자" | |
//jobTitle = "다른 직업" let으로 변수를 선언했기 때문에 값을 변경할 수 없어서 에러가 발생한다. | |
func sayName(name: String) { | |
print("안녕! 난 \(name)라고 해") | |
} | |
// 매개 변수에 inout을 작성하면 name이라는 매개변수의 값을 변경할 수 있다. | |
func sayHi(name: inout String) { | |
name = "개발하는 " + name | |
print("안녕! 난 \(name)라고 해") | |
} | |
sayName(name: "KH") | |
var name = "KKH " | |
sayHi(name: &name) | |
실행결과 | |
안녕! 난 KH라고 해 | |
안녕! 난 개발하는 KKH 라고 해 |
반응형