[Swift ๋ฌธ๋ฒ•] 07. Swift ํ•จ์ˆ˜ ๊ธฐ์ดˆ ํŒŒ๊ณ ๋“ค๊ธฐ 1 : ํ•จ์ˆ˜ ์„ ์–ธ, ์ผ๊ธ‰๊ฐ์ฒด?

2021. 1. 9. 18:40ใ†๐Ÿง‘๐Ÿป‍๐Ÿ’ป iOS ๊ฐœ๋ฐœ/Swift

ํ•ด๋‹น ๊ธ€์€ ์•ผ๊ณฐ๋‹˜์˜ ์Šค์œ„ํ”„ํŠธ ๊ฐ•์˜์™€ ๊ฐœ์ธ์ ์ธ ๊ณต๋ถ€๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.


์ฒ˜์Œ ํฌ์ŠคํŒ…์—์„œ Swift๋Š” ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจ๋Ÿฌ๋‹ค์ž„์ด๋ผ๊ณ  ์–ธ๊ธ‰ํ–ˆ์—ˆ๋˜ ๊ฒƒ ๊ธฐ์–ต๋‚˜์‹œ๋‚˜์š”? ๊ทธ๋งŒํผ Swift์˜ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์ž˜ ๊ณต๋ถ€ํ•˜๊ณ  ๋„˜์–ด๊ฐ€์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Swift์˜ ๊ธฐ์ดˆ์ ์ธ ํ•จ์ˆ˜, ๊ทธ ํŠน์ง•๊ณผ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจ๋Ÿฌ๋‹ค์ž„์—์„œ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š” ๊ฐœ๋…์ธ ์ผ๊ธ‰ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. Swift์˜ ํ•จ์ˆ˜ 

Swift์˜ ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•จ์ˆ˜ ์ด๋ฆ„, ๋งค๊ฐœ ๋ณ€์ˆ˜ (Prameter), ๋ฐ˜ํ™˜ํƒ€์ž… (Return Type)๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ง์€ ์ด ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋งŒ ๋‹ฌ๋ผ๋„ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋ผ๋Š” ์˜๋ฏธ๊ฒ ์ฃ ?

  • Swift์˜ ํ•จ์ˆ˜๋Š” ์žฌ์ •์˜ ( ์˜ค๋ฒ„๋ผ์ด๋“œ: Override ), ์ค‘๋ณต ์ •์˜ ( ์˜ค๋ฒ„๋กœ๋“œ: Overload )๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

  • ํ•จ์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํŠน์ • ํƒ€์ž…์— ์—ฐ๊ด€๋˜์–ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅผ ๋ฟ์ž…๋‹ˆ๋‹ค.

1. 1. ํ•จ์ˆ˜ ์ •์˜

//Swift ํ•จ์ˆ˜ ์ •์˜ ๊ธฐ๋ณธํ˜•ํƒœ

func ํ•จ์ˆ˜์ด๋ฆ„(๋งค๊ฐœ๋ณ€์ˆ˜...) -> ๋ฐ˜ํ™˜ ํƒ€์ž… {
	์‹คํ–‰ ๊ตฌ๋ฌธ
    return ๋ฐ˜ํ™˜ ๊ฐ’
}

ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ํ‚ค์›Œ๋“œ๋Š” func์ด๋ฉฐ ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ๋ช…์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ -> ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ๋„์–ด์“ฐ๊ธฐ์— ํ•ญ์ƒ ์œ ์˜ํ•ด์ฃผ์„ธ์š”. ๋ฐ˜ํ™˜์„ ์œ„ํ•œ ํ‚ค์›Œ๋“œ๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ return์ž…๋‹ˆ๋‹ค. 

func helloWorld() -> String {
    return "Hello World!"
}

print(helloWorld()) 
// Hello World!

1. 2. ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„ ๊ณผ ์ „๋‹ฌ ์ธ์ž ๋ ˆ์ด๋ธ”

C, C++ ์–ธ์–ด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ Swift์—์„œ๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ํƒ€์ž… ์•ž์— ์ ์Šต๋‹ˆ๋‹ค. ๋ฆฌํ„ด ํƒ€์ž…๋„ -> ๋’ค๋กœ ๋งจ ๋’ค์— ์œ„์น˜ํ•˜์ฃ .

( C++ : int sum(int a, int b)์ด์—ˆ๋‹ค๋ฉด, Swift : func sum(a: Int, b: Int) -> Int์˜ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. )

ํ—ท๊ฐˆ๋ฆฌ์‹œ๋‹ค๋ฉด ์˜ˆ์‹œ๋ฅผ ํ•œ๋ฒˆ ๋ด…์‹œ๋‹ค.

func sumInt(a: Int, b: Int) -> Int {
    return a + b
}

var someInt: Int = sumInt(a: 3, b: 4)

print(someInt)                       // 7

func sayName(name: String) -> Void {
    print("์ œ ์ด๋ฆ„์€ \(name) ์ž…๋‹ˆ๋‹ค.")
    return
}

sayName(name: "extramilejin")       
// ์ œ ์ด๋ฆ„์€ extramilejin ์ž…๋‹ˆ๋‹ค.

์ „๋‹ฌ ์ธ์ž ๋ ˆ์ด๋ธ”์€ ๊ฐ๊ฐ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์—ญํ• ์„ ์ข€ ๋” ๋ช…ํ™•ํžˆ ์„ค๋ช…ํ•ด์ค๋‹ˆ๋‹ค. ์ „๋‹ฌ ์ธ์ž ๋ ˆ์ด๋ธ”์€ ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•œ ์นธ ์•ž์— ์ ์–ด ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ „๋‹ฌ ์ธ์ž ๋ ˆ์ด๋ธ”์„ ์ž˜ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋ณผ ๋•Œ ํ•จ์ˆ˜๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ ๊ฒ๋‹ˆ๋‹ค.

// ์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ” 

func sayHello(from me: String, to friendName: String) -> String {
    return "Hello! \(friendName). I'm \(me)."
}

print(sayHello(from: "extramilejin", to: "smith")) 
// Hello! smith. I'm extramilejin.

์œ„์˜ ์˜ˆ์‹œ์—์„œ ์ „๋‹ฌ ์ธ์ž ๋ ˆ์ด๋ธ”์€ from ๊ณผ to ์ž…๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์€ me์™€ friendName์ด ๋˜๊ฒ ์ฃ . ์ด์ฒ˜๋Ÿผ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„ ํ•œ์นธ ์•ž์— ์“ฐ์—ฌ์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์—ญํ• ์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ์—๋„ ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋ฆ„ ๋Œ€์‹  ์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ, ๋‚˜๋จธ์ง€๋Š” ๋‹ค ๊ฐ™์€ ํ•จ์ˆ˜์—ฌ๋„ ์ด ์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”์ด ๋‹ค๋ฅด๋ฉด ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ฆ‰, ์ „๋‹ฌ์ธ์ž ๋ ˆ์ด๋ธ”๋งŒ์„ ๋ฐ”๊ฟ” ์จ์ค˜๋„ ํ•จ์ˆ˜ ์ค‘๋ณต ์ •์˜(์˜ค๋ฒ„๋กœ๋“œ)๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


1. 3. ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ธฐ๋ณธ๊ฐ’๊ณผ ๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜  

๋‹ค๋ฅธ ์–ธ์–ด๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Swift์˜ ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•˜๋ฉด ์ „๋‹ฌ ์ธ์ž๋กœ ๊ฐ’์ด ๋„˜์–ด์˜ค์ง€ ์•Š์•„๋„ ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ •์ƒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ํƒ€์ž… ๋’ค์— = ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

func repeatSayHello(from me: String, to friendName: String, repeatCount times: Int = 2) -> Void {
    for i in 0..<times {
        print("Hello! \(friendName) I'm \(me).")
    }
}

repeatSayHello(from: "extramilejin", to: "simson") 
// Hello! simson I'm extramilejin.
// Hello! simson I'm extramilejin.

times ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์„ 2๋กœ ์„ค์ •ํ•˜์—ฌ ํ˜ธ์ถœ ์‹œ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ ์ •์ƒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.


๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ช‡ ๊ฐœ์˜ ๊ฐ’์ด ๋“ค์–ด์˜ฌ์ง€ ๋ชจ๋ฅผ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 0๊ฐœ ์ด์ƒ์˜ ๊ฐœ์ˆ˜ ์ผ ๋•Œ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ 0๊ฐœ์ผ ๋•Œ, ๊ฐ’์ด ๋“ค์–ด์˜ค์ง€ ์•Š์•˜์„ ๋•Œ๋„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ž… ๋’ค์— ๋งˆ์นจํ‘œ ์„ธ ๊ฐœ(...)๋ฅผ ์ฐ์–ด ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.

์ค‘์š”ํ•œ ์ ์€ ํ•จ์ˆ˜๋Š” ๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

๊ฐ€๋ณ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ ์ธ์ž๋Š” ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

func sayHelloFriends(from me: String, to friendNames: String...) -> String {
   var result: String = ""
   
   for friendName in friendNames {
   		result += "Hello \(friendName)! "
   }
   result += "I'm \(me)."
   return result
}

print(sayHelloFriends(from: "extramilejin", to: "simson","naruto","duli")) 

// Hello simson! Hello naruto! Hello duli! I'm extramilejin.

2. ์ผ๊ธ‰ ๊ฐ์ฒด ( First-Class Citizen )

Swift ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ํŠน์ง•์€ ํ•จ์ˆ˜๋ฅผ ์ผ๊ธ‰ ๊ฐ์ฒด ( First-Class Citizen )๋กœ ๋‹ค๋ฃฌ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

์ผ๋“ฑ๊ธ‰ ์‹œ๋ฏผ? ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ฒ˜์Œ ์ ‘ํ•˜์‹œ๋Š” ๋ถ„๋“ค์€ ์ƒ์†Œํ•œ ๊ฐœ๋…์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ถ„์•ผ์—์„œ ์ผ๊ธ‰ ๊ฐ์ฒด๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ์ถฉ์กฑํ•ด์•ผ ํ•  5๊ฐ€์ง€ ์กฐ๊ฑด๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. 

  • ๋ณ€์ˆ˜๋‚˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์•ˆ์— ๋‹ด์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ „๋‹ฌ ์ธ์ž (Argument)๋กœ ์ „๋‹ฌ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • ๋™์  ํ”„๋กœํผํ‹ฐ ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • ํ• ๋‹นํ•  ๋•Œ ์‚ฌ์šฉ๋œ ์ด๋ฆ„๊ณผ ๊ด€๊ณ„์—†์ด ๊ณ ์œ ํ•œ ๊ฐ์ฒด๋กœ ๊ตฌ๋ณ„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฐ ๊ฒƒ๋“ค์ด ๊ฐ€๋Šฅํ•˜๋ฉด ์ผ๊ธ‰ ๊ฐ์ฒด๋ผ๊ณ  ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๊ตฌ๋‚˜๋ผ๊ณ  ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐ€์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

2. 1. ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜๋‚˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์•ˆ์— ๋‹ด์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

func addInt(first a: Int, second b: Int) -> Int {
    return a + b
}

var someFunc: (Int, Int) -> Int = addInt

print(someFunc(2,3)) 
// 5

2. 2. ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

func addInt(a: Int, b: Int) -> Int {
    return a + b
}
func substractInt(a: Int, b: Int) -> Int {
    return a - b
}

// ์ „๋‹ฌ ๋ฐ›์€ Bool ๊ฐ’์— ๋”ฐ๋ผ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

func chooseMathFuction(toAdd: Bool) -> (Int, Int) -> Int {
    return toAdd ? addInt : substractInt
} 

var mathFunction: (Int, Int) -> (Int) = chooseMathFuction(toAdd: true)

print(mathFunction(5,3))    // 8

mathFunction = chooseMathFuction(toAdd: false)

print(mathFunction(5,3))    // 2

2. 3. ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ ์ธ์ž๋กœ ์ „๋‹ฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค

func addInt(a: Int, b: Int) -> Int {
    return a + b
}

// ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ ๋ฐ›์Šต๋‹ˆ๋‹ค.

func outsideFunc(function: (Int, Int) -> Int) -> Int {
    var result: Int = function(3,2)
    return result
}

print(outsideFunc(function: addInt))

3. ๋งˆ๋ฌด๋ฆฌ 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ Swift์˜ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜ ํ˜•ํƒœ์™€ ์ผ๊ธ‰ ๊ฐ์ฒด ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜, ์ค‘์ฒฉ ํ•จ์ˆ˜์™€ ๋ฐ˜ํ™˜์ด ์—†๋Š” ํ•จ์ˆ˜ ๋“ฑ์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

์ฐธ์กฐ: ์• ํ”Œ API ๊ฐ€์ด๋“œ๋ผ์ธ , ์• ํ”Œ ๊ณต์‹ ๊ฐœ๋ฐœ ๋ฌธ์„œ , Swift 5 ํ”„๋กœ๊ทธ๋ž˜๋ฐ/ ์ง€์€์ด ์•ผ๊ณฐ