1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import UIKit func sayHello(){ print("Hello") } sayHello() //Hello func sayHello2(name:String){ print("Hello \(name)") } sayHello2(name: "Choi") //Hello Choi func sayHello3(name:String) -> String{ return "Hello " + name } print(sayHello3(name: "Choi")) //Hello Choi //default value func sayHello4(name:String = "Park"){ print("Hello \(name)") } sayHello4() //Hello Park sayHello4(name: "Jin") //Hello Jin //argument label func sayHello5(insertYourName name:String, internationalAge age:Int){ print("Hello \(name) is \(age) years old.") } sayHello5(insertYourName: "Choi", internationalAge: 10) //Hello Choi is 10 years old. //_ 기호 func sayHello6(name:String, _ age:Int) -> String{ return "\(name) is \(age) years old." } print(sayHello6(name: "Jin", 5)) //Jin is 5 years old. |
함수의 파라미터는 argument label과 parameter name을 가지는데
argument label는 함수를 호출할 때 쓰이고 parameter name은 함수 안의 변수로서 쓰인다.
따로 구분지어 쓰지 않을 경우 하나의 이름이 argument label도 되고 parameter name도 될 수 있다.
함수를 호출할 때 java나 c처럼 argument label을 쓰기 싫다면 _(언더바) 를 써주면 된다.
'프로그래밍 > Swift | iOS' 카테고리의 다른 글
[Swift3 & iOS10] struct(구조체) (0) | 2017.12.01 |
---|---|
[Swift3 & iOS10] class, property observer (0) | 2017.12.01 |
[Swift3 & iOS10] 조건문 if,switch (0) | 2017.12.01 |
[Swift3 & iOS10] 반복문 for,while (0) | 2017.11.30 |
[Swift3 & iOS10] Dictionary (0) | 2017.11.30 |