VesselWheel

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

Xcode Study

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

JasonYang 2023. 10. 14. 08:03

import UIKit

 

// function type - 함수의 자료형

/*

   First-class Citizen

 

   1) 함수를 변수나 상수에 할당할 수 있음

   2) 함수를 parameter로 전달할 수 있음

   3) 함수를 return할 수 있음

 */

 

func test1(){

    print("swift5")

}

test1()

 

let f1 = test1

f1()

 

// displayHello with 함수 : displayHello(with:)

func displayHello(with name: String){

    print("Hello, \(name)")

}

 

let f2: (String) -> () = displayHello(with:)

 

let f3 = displayHello(with:)

 

displayHello(with: "더조은")

// argument label 이 있는 함수를 변수에 할당한 후

// 호출할 때는 argument label 을 사용하지 않음

// f3(with: "이순신")

f3("이순신")

// f2(with: "이이")

f2("유관순")

 

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

    return n1 + n2

}

var f4: (Int, Int) -> Int = add

print(f4(1, 2))

 

var f5 = add

print(f5(10, 20))

 

/*

 error: cannot find 'add(n1:n2:)' in scope

var f6: (Int, Int) -> Int = add(n1:n2:)

 print(f6(3, 4))

*/

 

 

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

    return n1 + n2

}

// argument label 이 있는 경우

var f7 = add2(_:with:)

print(f7(11, 22))

 

// parameter 에 모두 argument label 을 선언한 경우

func swapNumbers(_ n1: inout Int, _ n2: inout Int){

    

}

let f8 = swapNumbers(_:_:)

f8

 

// 가변 파라미터함수

func sum(of number: Int...){

    

}

let f9 = sum(of: )

f9

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

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