아미(아름다운미소)

[문법요약] 함수 본문

랭귀지/SWIFT

[문법요약] 함수

유키공 2017. 12. 13. 12:54

- 함수

//: Playground - noun: a place where people can play
import UIKit

let name = "BTS"

func printName() {
    print(name)
}

func printName(userName: String) {
    print(userName)
}

//func printName(userName: String) -> Void {
//    print(userName)
//}

func printName2(userName: String) -> Int {
    print(userName)
    return userName.characters.count
}

//진 얼앰 슈가 뷔 제이홉 정국 지민
printName()
printName(userName: "선동렬")
printName2(userName: "진")
printName2(userName: "얼앰")
printName2(userName: "슈가")
printName2(userName: "뷔")
printName2(userName: "제이홉")
printName2(userName: "정국")
printName2(userName: "지민")

func sum(lhs: Int, rhs: Int) -> Int {
    let s = lhs + rhs
    return s
}
sum(lhs: 10, rhs: 7)
sum(lhs: 20, rhs: 5)

func sum2(leftHandSide lhs: Int, rightHandSide rhs: Int) -> Int {
    let s = lhs + rhs
    return s
}

sum2(leftHandSide: 10, rightHandSide: 20)

func sum3(_ lhs: Int, rightHandSide rhs: Int) -> Int {
    let s = lhs + rhs
    return s
}

sum3(10, rightHandSide: 10)

func sum4(_ lhs: Int, _ rhs: Int) -> Int {
    let s = lhs + rhs
    return s
}

sum4(10, 10)

func sumAll(number: Int...) -> Int {
    var sum = 0
    for i in number {
        sum += i
    }
    return sum
}

sumAll(number: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

// 튜플
let myInfo: [String: String] = ["이름": "공유", "이메일": "seungrry@daum.net", "핸드폰": "8940-4201"]

func getName() -> String? {
    return myInfo["이름"]
}

func getEmail() -> String? {
    return myInfo["이메일"]
}

func getPhoneNumber() -> String? {
    return myInfo["핸드폰"]
}

getName()
getEmail()
getPhoneNumber()

func getMyInfo() -> (String?, String?, String?, Int) {
    return (myInfo["이름"], myInfo["이메일"], myInfo["핸드폰"], myInfo.count)
}


let (myName, email, phone, infoCount) = getMyInfo()
print("이름: \(myName), 이메일: \(email), 핸드폰: \(phone), 정보수: \(infoCount)")


Comments