VesselWheel

function type - 함수의 자료형(2/2) 본문

Xcode Study

function type - 함수의 자료형(2/2)

JasonYang 2023. 10. 16. 14:35

import UIKit

 

func add(_ n1: Int, _ n2: Int) -> Int{

    return n1 + n2

}

func subtract(_ n1: Int, _ n2: Int) -> Int{

    return n1 - n2

}

func multiply(_ n1: Int, _ n2: Int) -> Int{

    return n1 * n2

}

func divide(_ n1: Int, _ n2: Int) -> Int{

    return n1 / n2

}

 

typealias ArithmeticFunction = (Int, Int) -> Int

 

func selectFunction(from op:String) ->

    ArithmeticFunction?{

        switch op{

        case "+":

            return add(_:_:)

        case "-":

            return subtract(_:_:)

        case "*":

            return multiply(_:_:)

        case "/":

            return divide(_:_:)

        default:

            return nil

        }

}

let addFunc = selectFunction(from: "+")

addFunc?(1, 2)

 

selectFunction(from: "*")?(12, 4)

'Xcode Study' 카테고리의 다른 글

Closure  (0) 2023.10.16
내부 함수 : nested functions  (0) 2023.10.16
function type - 함수의 자료형(1/2)  (1) 2023.10.14
In-Out paramters// 입출력 파라미터  (0) 2023.10.14
가변 parameters(variadic parameters) / 4-4  (1) 2023.10.14