[Swift 3] 함수 정의와 호출 및 함수 타입 (Function)
출처
함수 정의와 호출
기본 형식
func greetAgain(person: String) -> String {
return "Hello again, " + person + "!"
}
print(greetAgain(person: "Anna"))
파라미터 없는 함수
func sayHelloWorld() -> String {
return "hello, world"
}
print(sayHelloWorld())
여러개의 파라미터를 가진 함수
func greet(person: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return greetAgain(person: person)
} else {
return greet(person: person) }
}
func greet(person : String) -> String {
return "Hello, \(person)!"
}
func greetAgain(person : String) -> String {
return "Hello again, \(person)!"
}
print(greet(person: "Tim", alreadyGreeted: true))
반환값이 없는 함수
func greet(person: String) {
print("Hello, \(person)!")
}
greet(person: "Dave")
반환값이 여러개인 함수
func minMax(array : [Int]) -> (min : Int, max : Int) {
var currentMin = array.first!
var currentMax = array.first!
for value in array {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
옵셔널 튜플 반환타입
func minMax(array : [Int]) -> (min : Int, max : Int)? {
if array.isEmpty {
return nil
}
var currentMin = array.first!
var currentMax = array.first!
for value in array {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
let array = [8, -6, 2, 109, 3, 71]
if let tuple = minMax(array: array1) {
print ("min : \(tuple.min) / max : \(tuple.max)")
} else {
print ("array empty")
}
매개변수 이름
func someFunction(firstParameterName: Int, secondParameterName: Int) {
}
someFunction(firstParameterName: 1, secondParameterName: 2)
인자 레이블
func someFunction(argumentLabel parameterName: Int) {
// In the function body, parameterName refers to the argument value
// for that parameter.
}
func greet(person : String, from hometown : String) -> String {
return "Hello! \(person)! Glad you could visit from \(hometown)"
}
print(greet(person: "Bill", from: "Cupertino"))
인자 레이블 생략하기
func someFunction(_ firstParameterName : String, secondParameterName : String) {
}
someFunction("Bill", secondParameterName: "")
기본 매개변수 값
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6)
someFunction(parameterWithoutDefault: 4)
입출력 파라미터 (In-Out)
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
var first = 3
var second = 107
swapTwoInts(&first, &second)
print("first : \(first) / second : \(second)")
함수 타입
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
return a * b
}
var function : (Int, Int) -> Int
function = multiplyTwoInts(_:_:)
function(2, 3)
매개변수 타입처럼 사용하는 함수 타입
func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("Result: \(mathFunction(a, b))")
}
printMathResult(multiplyTwoInts, 3, 5)
반환 타입처럼 사용하는 함수 타입
func stepForward(_ input: Int) -> Int {
return input + 1
}
func stepBackward(_ input: Int) -> Int {
return input - 1
}
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
return backward ? stepBackward : stepForward
}
중첩된 함수
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backward ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
while currentValue != 0 {
print("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
print("zero!")