λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
iOS 🍎/Swift

Swift-03 ν•¨μˆ˜

by yongmin.Lee 2021. 1. 26.

ν•¨μˆ˜μ„ μ–Έμ˜ κΈ°λ³Έν˜•νƒœ

 func ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜1이름: λ§€κ°œλ³€μˆ˜1νƒ€μž…, λ§€κ°œλ³€μˆ˜2이름: λ§€κ°œλ³€μˆ˜2νƒ€μž… ...) -> λ°˜ν™˜νƒ€μž… {
 	/* ν•¨μˆ˜ κ΅¬ν˜„λΆ€ */
 	return λ°˜ν™˜κ°’
 }

 // 예1)

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

 // 예2 : λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” ν•¨μˆ˜)
func maximumIntegerValue() -> Int {
	return Int.max
 }

 

 λ°˜ν™˜ 값이 μ—†λŠ” ν•¨μˆ˜

 func ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜1이름: λ§€κ°œλ³€μˆ˜1νƒ€μž…, λ§€κ°œλ³€μˆ˜2이름: λ§€κ°œλ³€μˆ˜2νƒ€μž… ...) -> Void {
 	/* ν•¨μˆ˜ κ΅¬ν˜„λΆ€ */
 	return // λ°˜ν™˜κ°’μ΄ 없더라도 return ν‚€μ›Œλ“œλŠ” κΈ°μž¬ν•˜μ—¬ ν•¨μˆ˜μ˜ μ’…λ£Œλ₯Ό λͺ…μ‹œ
 }

 // 예)

func printMyName(name: String) -> Void {
    print(name)
    return
 }

 

λ§€κ°œλ³€μˆ˜ κΈ°λ³Έ κ°’

 func ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜1이름: λ§€κ°œλ³€μˆ˜1νƒ€μž…, λ§€κ°œλ³€μˆ˜2이름: λ§€κ°œλ³€μˆ˜2νƒ€μž… = λ§€κ°œλ³€μˆ˜ κΈ°λ³Έκ°’ ...) -> λ°˜ν™˜νƒ€μž… {
	 /* ν•¨μˆ˜ κ΅¬ν˜„λΆ€ */
	 return λ°˜ν™˜κ°’
 }

// 예
 func greeting(friend: String, me: String = "yagom") {
	 print("Hello \(friend)! I'm \(me)")
 }

 

μ „λ‹¬μΈμž λ ˆμ΄λΈ”

ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ ν•¨μˆ˜ μ‚¬μš©μžμ˜ μž…μž₯μ—μ„œ λ§€κ°œλ³€μˆ˜μ˜ 역할을 μ’€ 더 λͺ…ν™•ν•˜κ²Œ ν‘œν˜„ν•˜κ³ μž ν•  λ•Œ μ‚¬μš©

μ „λ‹¬μΈμž λ ˆμ΄λΈ”μ„ λ³€κ²½ν•˜μ—¬ λ™μΌν•œ μ΄λ¦„μ˜ ν•¨μˆ˜λ₯Ό μ€‘λ³΅μœΌλ‘œ 생성가λŠ₯

 func ν•¨μˆ˜μ΄λ¦„(μ „λ‹¬μΈμž λ ˆμ΄λΈ” λ§€κ°œλ³€μˆ˜1이름: λ§€κ°œλ³€μˆ˜1νƒ€μž…, μ „λ‹¬μΈμž λ ˆμ΄λΈ” λ§€κ°œλ³€μˆ˜2이름: λ§€κ°œλ³€μˆ˜2νƒ€μž… ...) -> λ°˜ν™˜νƒ€μž… {
	 /* ν•¨μˆ˜ κ΅¬ν˜„λΆ€ */
	 return
 }

 // "ν•¨μˆ˜ λ‚΄λΆ€"μ—μ„œ μ „λ‹¬μΈμžλ₯Ό μ‚¬μš©ν•  λ•Œμ—λŠ” "λ§€κ°œλ³€μˆ˜ 이름"을 μ‚¬μš©ν•©λ‹ˆλ‹€
 func greeting(to friend: String, from me: String) {
	 print("Hello \(friend)! I'm \(me)")
 }

 // "ν•¨μˆ˜λ₯Ό 호좜" ν•  λ•Œμ—λŠ” μ „λ‹¬μΈμž "λ ˆμ΄λΈ”"을 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€
 greeting(to: "hana", from: "yagom") // Hello hana! I'm yagom

 

 κ°€λ³€ λ§€κ°œλ³€μˆ˜

 μ „달 받을 κ°’μ˜ 개수λ₯Ό μ•ŒκΈ° μ–΄λ €μšΈ λ•Œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€

 κ°€λ³€ λ§€κ°œλ³€μˆ˜λŠ” ν•¨μˆ˜λ‹Ή ν•˜λ‚˜λ§Œ κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€

 func ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜1이름: λ§€κ°œλ³€μˆ˜1νƒ€μž…, μ „λ‹¬μΈμž λ ˆμ΄λΈ” λ§€κ°œλ³€μˆ˜2이름: λ§€κ°œλ³€μˆ˜2νƒ€μž…...) -> λ°˜ν™˜νƒ€μž… {
	 /* ν•¨μˆ˜ κ΅¬ν˜„λΆ€ */
	 return
 }

func sayHelloToFriends(me: String, friends: String...) -> String { 
	return "Hello \(friends)! I'm \(me)!" 
} 

print(sayHelloToFriends(me: "yagom", friends: "hana", "eric", "wing")) 
// Hello ["hana", "eric", "wing"]! I'm yagom! 

print(sayHelloToFriends(me: "yagom")) 
// Hello []! I'm yagom!

 

 λ°μ΄ν„° νƒ€μž…μœΌλ‘œμ„œμ˜ ν•¨μˆ˜

μŠ€μœ„ν”„νŠΈλŠ” ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° νŒ¨λŸ¬λ‹€μž„μ„ ν¬ν•¨ν•˜λŠ” 닀쀑 νŒ¨λŸ¬λ‹€μž„ μ–Έμ–΄μ΄λ―€λ‘œ μŠ€μœ„ν”„νŠΈμ˜ ν•¨μˆ˜λŠ” μΌκΈ‰κ°μ²΄μž…λ‹ˆλ‹€.

κ·Έλž˜μ„œ ν•¨μˆ˜λ₯Ό λ³€μˆ˜, μƒμˆ˜ 등에 할당이 κ°€λŠ₯ν•˜κ³  λ§€κ°œλ³€μˆ˜λ₯Ό 톡해 전달할 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€

// ν•¨μˆ˜νƒ€μž… : (λ§€κ°œλ³€μˆ˜1νƒ€μž…, λ§€κ°œλ³€μˆ˜2νƒ€μž… ...) -> λ°˜ν™˜νƒ€μž…
var someFunction: (String, String) -> Void = greeting(to:from:)

 someFunction("eric", "yagom") 
 // Hello eric! I'm yagom