μλ νμΈμ. μ€λμ ν¨μ(Functions)μ λν΄ μμλ³΄κ² μ΅λλ€. ν¨μλ νΉμ μμ μ μννλ μ½λ λΈλ‘μΌλ‘, μ½λμ μ¬μ¬μ©μ±κ³Ό κ°λ μ±μ λμ΄λ λ° λ§€μ° μ€μν©λλ€.
π ν¨μ μ μμ νΈμΆ
κΈ°λ³Έ ν¨μ μ μ
Swiftμμ ν¨μλ func ν€μλλ₯Ό μ¬μ©νμ¬ μ μν©λλ€:
func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
}
μ¬κΈ°μ:
- func: ν¨μλ₯Ό μ μνλ ν€μλ
- greet(person:): ν¨μ μ΄λ¦κ³Ό νλΌλ―Έν° λ μ΄λΈ
- -> String: λ°ν νμ
- return: ν¨μκ° κ°μ λ°ννλ ν€μλ
ν¨μ νΈμΆ
ν¨μλ μ΄λ¦κ³Ό μ μ ν μΈμ(arguments)λ₯Ό μ¬μ©νμ¬ νΈμΆν©λλ€:
print(greet(person: "Anna"))
// "Hello, Anna!" μΆλ ₯
ν¨μ(Function): νΉμ μμ μ μννλ λ 립μ μΈ μ½λ λΈλ‘
νλΌλ―Έν°(Parameter): ν¨μ μ μ μ μ¬μ©λλ μ λ ₯ κ°μ μ΄λ¦
μΈμ(Argument): ν¨μ νΈμΆ μ μ λ¬νλ μ€μ κ°
π ν¨μ νλΌλ―Έν°μ λ°νκ°
Swift ν¨μλ νλΌλ―Έν°μ λ°νκ°μ μ¬μ©μ΄ λ§€μ° μ μ°ν©λλ€.
νλΌλ―Έν° μλ ν¨μ
func sayHelloWorld() -> String {
return "hello, world"
}
print(sayHelloWorld()) // "hello, world" μΆλ ₯
μ¬λ¬ νλΌλ―Έν°λ₯Ό κ°μ§ ν¨μ
func greet(person: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return greetAgain(person: person)
} else {
return greet(person: person)
}
}
print(greet(person: "Tim", alreadyGreeted: true))
λ°νκ° μλ ν¨μ
func greet(person: String) {
print("Hello, \(person)!")
}
greet(person: "Dave") // "Hello, Dave!" μΆλ ₯
void λ°ν νμ : ν¨μκ° κ°μ λ°ννμ§ μμ λ μ¬μ©λ©λλ€(Swiftμμλ () λλ Voidλ‘ νν).
μ¬λ¬ κ°μ λ°ννλ ν¨μ
ννμ μ¬μ©νμ¬ μ¬λ¬ κ°μ ν¨κ» λ°νν μ μμ΅λλ€:
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
print("μ΅μκ°μ \(bounds.min), μ΅λκ°μ \(bounds.max)")
// "μ΅μκ°μ -6, μ΅λκ°μ 109" μΆλ ₯
νν(Tuple): μ¬λ¬ κ°μ νλμ λ³΅ν© κ°μΌλ‘ κ·Έλ£Ήννλ λ°©λ²
μ΅μ λ νν λ°ν νμ
μ 체 ννμ΄ nilμΌ μ μλ κ²½μ°, μ΅μ λ νν λ°ν νμ μ μ¬μ©ν μ μμ΅λλ€:
func minMax(array: [Int]) -> (min: Int, max: Int)? {
if array.isEmpty { return nil }
var currentMin = array[0]
var currentMax = array[0]
// ... ꡬν λ΄μ© μλ΅ ...
return (currentMin, currentMax)
}
if let bounds = minMax(array: [8, -6, 2, 109, 3, 71]) {
print("μ΅μκ°μ \(bounds.min), μ΅λκ°μ \(bounds.max)")
}
μ΅μ λ νν: μ 체 ννμ΄ nilμΌ μ μμμ λνλ΄λ νμ ((Type1, Type2)?)
μμμ λ°νμ κ°μ§ ν¨μ
ν¨μ λ³Έλ¬Έμ΄ λ¨μΌ ννμμΈ κ²½μ°, return ν€μλλ₯Ό μλ΅ν μ μμ΅λλ€:
func greeting(for person: String) -> String {
"Hello, " + person + "!"
}
μμμ λ°ν(Implicit Return): λ¨μΌ ννμμΌλ‘ ꡬμ±λ ν¨μμμ return ν€μλλ₯Ό μλ΅ν μ μλ κΈ°λ₯
π ν¨μ μΈμ λ μ΄λΈκ³Ό νλΌλ―Έν° μ΄λ¦
Swift ν¨μλ μΈμ λ μ΄λΈ(argument label)κ³Ό νλΌλ―Έν° μ΄λ¦(parameter name)μ λͺ¨λ κ°μ§ μ μμ΅λλ€.
μΈμ λ μ΄λΈκ³Ό νλΌλ―Έν° μ΄λ¦
func someFunction(argumentLabel parameterName: Int) {
// ν¨μ λ΄μμλ parameterNameμΌλ‘ μ κ·Ό
}
// νΈμΆ μμλ argumentLabel μ¬μ©
someFunction(argumentLabel: 1)
μΈμ λ μ΄λΈ μ§μ νκΈ°
func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// "Hello Bill! Glad you could visit from Cupertino." μΆλ ₯
μΈμ λ μ΄λΈ(Argument Label): ν¨μ νΈμΆ μ μ¬μ©λλ μ΄λ¦
νλΌλ―Έν° μ΄λ¦(Parameter Name): ν¨μ λ΄λΆμμ μ¬μ©λλ μ΄λ¦
μΈμ λ μ΄λΈ μλ΅νκΈ°
μΈλλ°(_)λ₯Ό μ¬μ©νμ¬ μΈμ λ μ΄λΈμ μλ΅ν μ μμ΅λλ€:
func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
// ν¨μ ꡬν
}
someFunction(1, secondParameterName: 2)
π νλΌλ―Έν° κΈ°λ³Έκ°
νλΌλ―Έν°μ κΈ°λ³Έκ°μ μ§μ ν μ μμ΄ νΈμΆ μ ν΄λΉ μΈμλ₯Ό μλ΅ν μ μμ΅λλ€:
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
// ν¨μ ꡬν
}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefaultλ 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefaultλ 12 (κΈ°λ³Έκ° μ¬μ©)
κΈ°λ³Έ νλΌλ―Έν° κ°(Default Parameter Value): μΈμκ° μ 곡λμ§ μμ λ μ¬μ©λλ κΈ°λ³Έκ°
π κ°λ³ νλΌλ―Έν°
κ°λ³ νλΌλ―Έν°(variadic parameter)λ 0κ° μ΄μμ νΉμ νμ κ°μ νμ©ν©λλ€:
func arithmeticMean(_ numbers: Double...) -> Double {
var total = 0.0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5) // 3.0 λ°ν
arithmeticMean(3, 8.25, 18.75) // 10.0 λ°ν
κ°λ³ νλΌλ―Έν°(Variadic Parameter): ν¨μ νΈμΆ μ μ¬λ¬ κ°μ μΈμλ₯Ό λ°μ μ μλ νλΌλ―Έν°λ‘, νμ λ€μ ...μ λΆμ¬ νμν©λλ€.
π In-Out νλΌλ―Έν°
ν¨μκ° νλΌλ―Έν° κ°μ μμ νκ³ κ·Έ λ³κ²½μ¬νμ ν¨μ νΈμΆμ΄ λλ νμλ μ μ§νκ³ μΆμ λ in-out νλΌλ―Έν°λ₯Ό μ¬μ©ν©λλ€:
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someIntλ μ΄μ \(someInt), anotherIntλ μ΄μ \(anotherInt)")
// "someIntλ μ΄μ 107, anotherIntλ μ΄μ 3" μΆλ ₯
in-out νλΌλ―Έν°: ν¨μ λ΄μμ μμ λκ³ ν¨μ μ’ λ£ νμλ μμ λ κ°μ΄ μ μ§λλ νλΌλ―Έν°
&κΈ°νΈ: in-out νλΌλ―Έν°μ μΈμλ₯Ό μ λ¬ν λ λ³μ μμ λΆμ΄λ κΈ°νΈ
π ν¨μ νμ
Swiftμμ ν¨μλ μ체μ μΈ νμ μ κ°μ§κ³ μμ΄ λ³μμ ν λΉνκ±°λ λ€λ₯Έ ν¨μμ νλΌλ―Έν°λ‘ μ λ¬ν μ μμ΅λλ€.
ν¨μ νμ μ¬μ©νκΈ°
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
return a * b
}
var mathFunction: (Int, Int) -> Int = addTwoInts
print("κ²°κ³Ό: \(mathFunction(2, 3))") // "κ²°κ³Ό: 5" μΆλ ₯
mathFunction = multiplyTwoInts
print("κ²°κ³Ό: \(mathFunction(2, 3))") // "κ²°κ³Ό: 6" μΆλ ₯
ν¨μ νμ (Function Type): ν¨μμ νλΌλ―Έν° νμ κ³Ό λ°ν νμ μ ν¬ν¨νλ νμ (μ: (Int, Int) -> Int)
νλΌλ―Έν° νμ μΌλ‘ ν¨μ νμ
ν¨μλ₯Ό λ€λ₯Έ ν¨μμ νλΌλ―Έν°λ‘ μ λ¬ν μ μμ΅λλ€:
func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("κ²°κ³Ό: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5) // "κ²°κ³Ό: 8" μΆλ ₯
λ°ν νμ μΌλ‘ ν¨μ νμ
ν¨μκ° λ€λ₯Έ ν¨μλ₯Ό λ°νν μλ μμ΅λλ€:
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
}
var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZeroλ μ΄μ stepBackward() ν¨μλ₯Ό μ°Έμ‘°
while currentValue != 0 {
print("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// 3... 2... 1... zero! μΆλ ₯
π μ€μ²© ν¨μ
Swiftμμλ ν¨μ λ΄λΆμ λ€λ₯Έ ν¨μλ₯Ό μ μν μ μμ΅λλ€:
func chooseStepFunction(backward: Bool) -> (Int) -> Int {
// μ€μ²© ν¨μ μ μ
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backward ? stepBackward : stepForward
}
var currentValue = -4
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
// moveNearerToZeroλ μ΄μ μ€μ²©λ stepForward() ν¨μλ₯Ό μ°Έμ‘°
while currentValue != 0 {
print("\(currentValue)... ")
currentValue = moveNearerToZero(currentValue)
}
print("zero!")
// -4... -3... -2... -1... zero! μΆλ ₯
μ€μ²© ν¨μ(Nested Function): λ€λ₯Έ ν¨μ λ΄λΆμ μ μλ ν¨μλ‘, κΈ°λ³Έμ μΌλ‘ μΈλΆμμ μ κ·Όν μ μμ§λ§ ν¬ν¨νλ ν¨μμ μν΄ λ°νλ μ μμ΅λλ€.
π μ 리
Swiftμ ν¨μλ λ§€μ° μ μ°νκ³ κ°λ ₯ν κΈ°λ₯μ μ 곡ν©λλ€:
- κ°λ μ± λμ ν¨μ νΈμΆ: μΈμ λ μ΄λΈμ ν΅ν΄ ν¨μ νΈμΆ μ κ° μΈμμ μ©λλ₯Ό λͺ νν ν μ μμ΅λλ€.
- μ μ°ν νλΌλ―Έν° μ΅μ : κΈ°λ³Έκ°, κ°λ³ νλΌλ―Έν°, in-out νλΌλ―Έν° λ± λ€μν νλΌλ―Έν° μ΅μ μ μ 곡ν©λλ€.
- μΌκΈ μλ―ΌμΌλ‘μμ ν¨μ: ν¨μλ₯Ό λ³μμ ν λΉνκ±°λ, νλΌλ―Έν°λ‘ μ λ¬νκ±°λ, λ°νκ°μΌλ‘ μ¬μ©ν μ μμ΅λλ€.
- μ€μ²© ν¨μ: ν¨μ λ΄λΆμ λ€λ₯Έ ν¨μλ₯Ό μ μνμ¬ μ½λμ μΊ‘μνμ μ¬μ¬μ©μ±μ λμΌ μ μμ΅λλ€.
μ΄λ¬ν κΈ°λ₯λ€μ μ μ ν νμ©νλ©΄ λ μ½κΈ° μ½κ³ μ μ§λ³΄μκ° μ©μ΄ν μ½λλ₯Ό μμ±ν μ μμ΅λλ€. νΉν ν¨μ νμ κ³Ό μ€μ²© ν¨μλ Swiftμ ν¨μν νλ‘κ·Έλλ° νΉμ±μ μ 보μ¬μ£Όλ κΈ°λ₯μ λλ€.
'π₯ Bread Basics > Swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Swift 곡μ λ¬Έμ μ 리 - νλ‘νΌν° (Properties) (0) | 2025.04.11 |
---|---|
Swift 곡μ λ¬Έμ μ 리 - ꡬ쑰체μ ν΄λμ€ (Structures and Classes) (0) | 2025.04.11 |
Swift 곡μ λ¬Έμ μ 리 - μ΄κ±°ν (Enumerations) (0) | 2025.04.11 |
Swift 곡μ λ¬Έμ μ 리 - ν΄λ‘μ (Closures) (0) | 2025.04.11 |
Swift 곡μ λ¬Έμ μ 리 - μ μ΄ νλ¦ (Control Flow) (0) | 2025.04.10 |
Swift 곡μ λ¬Έμ μ 리 - 컬λ μ νμ (Collection Types) (0) | 2025.04.10 |
Swift 곡μ λ¬Έμ μ 리 - λ¬Έμμ΄κ³Ό λ¬Έμ (Strings and Characters) (0) | 2025.04.10 |
Swift 곡μ λ¬Έμ μ 리 - κΈ°λ³Έ μ°μ°μ (Basic Operators) (0) | 2025.04.10 |