[Swift] ν¨μ(Function) - κΈ°λ³Έ
βοΈ ν¨μμ μ μ
μ ν κ°λ°λ¬Έμμ λ°λ₯΄λ©΄ Swiftμμ ν¨μλ λ€μκ³Ό κ°μ΄ μ μλλ€.
[Definition]
Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.
ν¨μ(Functions)λ νΉμ μμ μ μννλ μ체μ μΌλ‘ ν¬ν¨λ(self-contained) μ½λ λ©μ΄λ¦¬(chunks) μ΄λ€. ν¨μκ° λ¬΄μμ νλμ§ μλ³κ°λ₯ν μ΄λ¦μ μ£Όκ³ , μ΄λ¬ν μ΄λ¦μ νμν λ μμ μ μννκΈ° μν΄ νΈμΆ(call)νμ¬ μ¬μ©λλ€.
- μλ¬Έ : μ ν κ°λ° λ¬Έμ Swift 4.2 Language Guide - Functions
Contents
1. ν¨μμ μ μΈ, νΈμΆνκΈ° 1 - 1. ν¨μμ μΈμ κΈ°λ³Ένν (Defaining Functions) 1 - 2. ν¨μ νΈμΆ (Calling Functions) 2. ν¨μμ νν 2 - 1. λ°νκ°μ΄ μλ ν¨μ (Functions Without Return Values) 2 - 2. 맀κ°λ³μκ° μλ ν¨μ (Functions Without Parameters) 2 - 3. λ°νκ°κ³Ό 맀κ°λ³μκ° μλ ν¨μ (Function Without Return Values and Parameters) 2 - 4. 맀κ°λ³μκ° μ¬λ¬κ°μΈ ν¨μ (Function With Mulitple Parameters) 2 - 5. λ°νκ°μ΄ μ¬λ¬κ°μΈ ν¨μ (Function With Mulitple Return Values) |
π ν¨μ(Function)μ μ μΈ, νΈμΆνκΈ°
[ν¨μμ μ μΈ(Defaining Functions)]
Swiftμμ ν¨μλ₯Ό μ μν λλκ°μ₯ μμ func ν€μλλ₯Ό λΆμ΄κ³ (person: String) νλΌλ―Έν°μ ν κ·Έλ¦¬κ³ -> String ννλ‘ λ°ννμ μ μνλ€.
μ΄ λ, ν¨μμμ νμ 맀κ°λ³μ(Parameters) νμ , ν μμ λ€μ΄κ°λ μ€μ κ°μ μΈμ(Arguments)λΌ νλ€.
func <ν¨μμ΄λ¦>(<맀κ°λ³μ1μ΄λ¦>: <맀κ°λ³μ1νμ
>, <맀κ°λ³μ2μ΄λ¦>: <맀κ°λ³μ2νμ
> ...) -> <λ°ννμ
> {
<ν¨μ ꡬνλΆ>
return <λ°νκ°>
}
func greet(person: String, age: Int) -> String {
let greeting = "Hello, " + person + "I'm" + String(age) + "!"
return greeting
}
[ν¨μ νΈμΆ(Calling Functions)]
ν¨μμ΄λ¦(맀κ°λ³μ1μ΄λ¦: μ
λ ₯κ°, 맀κ°λ³μ2μ΄λ¦: μ
λ ₯κ° ...)
print(greet(person: "Anna", age: 20))
π ν¨μ(Function)μ νν
2 - 1. λ°νκ°μ΄ μλ ν¨μ (Functions Without Return Values)
ν¨μλ λ°νκ°μ μλ΅ν μ μλ€. λ°νκ°μ΄ μκΈ° λλ¬Έμ -> κ³Ό ν¨κ» λ°ννμ λ μλ΅ κ°λ₯νλ€.
func ν¨μμ΄λ¦(맀κ°λ³μ1μ΄λ¦: 맀κ°λ³μ1νμ
, 맀κ°λ³μ2μ΄λ¦: 맀κ°λ³μ2νμ
...) -> Void {
/* ν¨μ ꡬνλΆ */
}
// void μλ΅κ°λ₯
func ν¨μμ΄λ¦(맀κ°λ³μ1μ΄λ¦: 맀κ°λ³μ1νμ
, 맀κ°λ³μ2μ΄λ¦: 맀κ°λ³μ2νμ
...) {
}
func printMyName(name: String) -> Void {
print(name)
}
//Void μλ΅ κ°λ₯
func printMyName(name: String){
print(name)
}
// νΈμΆ
printYourName(name: "lee")
// >> lee
2 - 2. νλΌλ―Έν°κ° μλ ν¨μ (Functions Without Parameters)
ν¨μλ μ λ ₯ 맀κ°λ³μλ₯Ό μλ΅ν μ μλ€. μλ΅νλλΌλ ν¨μ μ΄λ¦ λ€μ μκ΄νΈ()λ μλ΅ν μ μλ€.
func ν¨μμ΄λ¦() -> λ°ννμ
{
/* ν¨μ ꡬνλΆ */
return λ°νκ°
}
func maximumIntegerValue() -> Int {
return Int.max
}
// νΈμΆ
maximumIntegerValue()
// >> 9223372036854775807
2 - 3. λ°νκ°κ³Ό 맀κ°λ³μκ° μλ ν¨μ (Function Without Return Values and Parameters)
λ¬Όλ‘ λ°νκ°κ³Ό μ λ ₯ 맀κ°λ³μ λͺ¨λ μλ΅ν μ μλ€.
func ν¨μμ΄λ¦() -> Void { // -> Void μλ΅ κ°λ₯
/* ν¨μ ꡬνλΆ */
return
}
// ν¨μ ꡬνμ΄ μ§§μ κ²½μ° κ°λ
μ±μ ν΄μΉμ§ μλ λ²μμμ μ€λ°κΏμ νμ§ μκ³ ν μ€μ ννν΄λ 무κ΄νλ€.
func hello() -> Void { print("hello") }
// νΈμΆ
hello()
// >> hello
2 - 4. 맀κ°λ³μκ° μ¬λ¬κ°μΈ ν¨μ (Function With Mulitple Parameters)
ν¨μλ μ½€λ§λ‘ ꡬλΆλ μ¬λ¬κ°μ νλΌλ―Έν°λ₯Ό κ°μ§ μ μλ€.
func greet(person: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return "Hello, " + person + ". again!"
}else{
return "Hello, " + person + "!"
}
}
//νΈμΆ
print(greet(person: "Tim", alreadyGreeted: false))
2 - 5. λ°νκ°μ΄ μ¬λ¬κ°μΈ ν¨μ (Function With Mulitple Return Values)
ν¨μλ λ°ν νμ μ νν κ°μ μ¬μ©ν μ μμΌλ©° νλμ μ§ν©μΌλ‘ λ λ€μ€ κ°μ λ°ννλ€.
func sum(_ num1: Int, _ num2: Int) -> (Int, Int, Int){
return (num1, num2, num1+num2)
}
var sumRes = sum(20, 30)
print(sumRes)
// >> (20, 30, 50) λͺ¨λ κ° μΆλ ₯
μνλ νΉμ λ°νκ°μ μΆλ ₯νκ³ μΆμ κ²½μ°μλ λκ°μ§ λ°©λ²μ΄ μλ€.
1. index λ²νΈλ‘ μ κ·Όνλ λ°©λ²
2. λ°νκ°μ λ³μλ₯Ό μ μΈνμ¬ λ³μλ‘ μ κ·Όνλ λ°©λ²
func sum(_ num1: Int, _ num2: Int) -> (Int, Int, res: Int){
return (num1, num2, num1+num2)
}
var sumRes = sum(20, 30)
// 1. index λ²νΈλ‘ μ κ·Ό
print(sumRes.0)
// >> 20
// 2. λ°νκ°μ λ³μλ‘ μ κ·Ό
print(sumRes2.res)
// >> 50
π 맀κ°λ³μμ κΈ°λ³Έ κ°(default value) μ€μ νκ³ νΈμΆ μ μλ΅νκΈ°
- 맀κ°λ³μμ 미리 κ°μ μ€μ ν΄λμ μ μμ
- κΈ°λ³Έκ°μ κ°λ 맀κ°λ³μλ 맀κ°λ³μ λͺ©λ‘ μ€μ λ€μͺ½μ μμΉ
- 맀κ°λ³μ κΈ°λ³Έκ°μ κ°μ§λ 맀κ°λ³μλ νΈμΆ μ μλ΅ κ°λ₯
- _λ₯Ό μ¬μ©νμ¬ λ§€κ°λ³μ μλ΅ κ°λ₯
func greeting(friend: String, me: String = "lee") {
print("Hello \(friend)! I'm \(me)")
}
//meλ κΈ°λ³Έκ°μ΄ μμΌλ―λ‘ μλ΅ κ°λ₯!
greeting(friend: "hana") // Hello hana! I'm lee
greeting(friend: "john", me: "eric") // Hello john! I'm eric
//(_)μ¬μ©νμ¬ parameter μ΄λ¦ μλ΅νκΈ°
func greeting(_ friend: String, from me: String) {
print("Hello \(friend)! I'm \(me)")
}
greeting("Bill", from: "") //Hello Bill! I'm
π μ λ¬μΈμ λ μ΄λΈ
- 맀κ°λ³μκ° μ΄λ€ μν μ ν κ±΄μ§ λͺ ννκ² ν΄ μ€
- 맀κ°λ³μ μ΄λ¦ μμ μμ±
- 곡백μΌλ‘ ꡬλΆ
//μ μ½λμ greetingκ³Ό λ€λ₯Έ ν¨μ
//μ λ¬μΈμ λ μ΄λΈ : to, from
func greeting(to friend: String, from me: String) {
print("Hello \(friend)! I'm \(me)")
// μ λ¬μΈμμ μν μ νννκ³ μΆμ λ μ¬μ©
// ν¨μ λ΄λΆμμ μ λ¬μΈμ(to, from) λ₯Ό μ¬μ©ν λμλ 맀κ°λ³μ μ΄λ¦μ μ¬μ©ν©λλ€
}
// ν¨μλ₯Ό νΈμΆν λμλ μ λ¬μΈμ λ μ΄λΈμ μ¬μ©
greeting(to: "hana", from: "lee") // Hello hana! I'm lee
π κ°λ³ 맀κ°λ³μ
- μ λ¬λ°μ κ°μ κ°μλ₯Ό μκΈ° μ΄λ €μΈ λ μ¬μ©ν μ μλ€.
- κ°λ³ 맀κ°λ³μλ ν¨μλΉ νλλ§ κ°μ§ μ μλ€.
- νμ 맨 λ€μ μμΉνλ©° ...μΌλ‘ νν
//μ λ¬ μΈμ μ¬λ¬κ° λ°μ μ μμ(νμ X)
//...μΌλ‘ νν
func sayHelloToFriends(me: String, friends: String...) -> String {
return "Hello \(friends)! I'm \(me)!"
}
print(sayHelloToFriends(me: "lee", friends: "one", "two", "three"))
// Hello ["one", "two", "three"]! I'm lee!
//μΈμ μ λ£μΌλ €λ©΄ μ λ¬μΈμ μμ μλ΅
print(sayHelloToFriends(me: "lee"))
// Hello []! I'm lee!
//맀κ°λ³μ μ¨λκ³ κ° μλ£μΌλ©΄ μ€λ₯ λ°μ
//print(sayHelloToFriends(me: "lee", friends: nil))
//print(sayHelloToFriends(me: "lee", friends: ))
βοΈ κ°λ³ 맀κ°λ³μλ‘ νκ· κ΅¬νκΈ°
func avg1(score: Int ...) -> Double{
var total = 0
for i in score{ //κ°λ³ μΈμμ λκΈ΄ κ°μ μ μ ν νμ
μ λ°°μ΄λ‘ λ§λ€μ΄μ§.
total += i
}
return Double(total)/Double(score.count)
}
var result = avg1(score: 1, 2, 3) //2
var a: [Int] = []
func avg2(score: Int ...) -> Double{
a = score // λ°°μ΄μ λμ
κ°λ₯
var total = 0
for i in score{ //κ°λ³ μΈμμ λκΈ΄ κ°μ μ μ ν νμ
μ λ°°μ΄λ‘ λ§λ€μ΄μ§.
total += i
}
return Double(total)/Double(score.count)
}
var result1 = avg2(score: 1, 2, 3) //2
//κ°λ³μΈμ μΆλ ₯
print("scores : \(a)") //scores : [1, 2, 3]
print("first score : \(a[0])") //first score : 1
π λ°μ΄ν° νμ μΌλ‘μμ ν¨μ
- μΈμ λ ν¨μλ parameter, return κ°μ λ°λ₯Έ νΉμ νμ μ κ°μ§λ€.
- Swiftμμλ ν¨μ νμ μ λ°λΌ λ³μλ₯Ό μ μν μ μλ€.
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
return a * b
}
var function : (Int, Int) -> Int
function = multiplyTwoInts(_:_:)
function(2, 3)
βοΈ λ§€κ°λ³μ νμ μ²λΌ μ¬μ©νλ ν¨μ νμ
- (Int, Int) -> (Int)μ κ°μ ν¨μ νμ μ λ€λ₯Έ ν¨μμ 맀κ°λ³μλ‘ μ¬μ©ν μλ μλ€.
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
}