์ƒˆ์†Œ์‹

๐Ÿ“ฑ iOS/-- Swift 5

(Swift) Function (ํ•จ์ˆ˜)

  • -

Function (ํ•จ์ˆ˜)

์ •์˜ ๋ฐ ํ˜ธ์ถœ (defining and calling functions)

swif์—์„œ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ• ๋•Œ๋Š” func ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  (person: String) ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…, ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜ํ™˜ํ˜•์„-> Stinrg ํ™”์‚ดํ‘œ ํ˜•ํƒœ๋กœ ์ •์˜ํ•œ๋‹ค.

func greet(person: String) -> String{
  let greeting = "Hello, " + person + "!"
  return greeting
}
print(greet(person: "JY"))
>>> Hello, JY!

ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋ฐ˜ํ™˜๊ฐ’(Function Parameters and Return Values)

func sayHelloWorld() -> String{
  return "hello, world"
}
print(sayHelloWorld())
  • ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†์–ด๋„ ๋œ๋‹ค.
  • ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ๋„ ๋œ๋‹ค.
  • ๋ฐ˜ํ™˜ ๊ฐ’์ด ์—†์–ด๋„ ๋œ๋‹ค. (์‹ค์ œ ๋™์ž‘์€ return 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 bound = minMax(array: [1,3,4,12,1,3,99,-7])
print(type(of: bound)) //(min: Int, max: Int)
print(bound) // (min: -7, max: 99)
print(bound.min) // 7
print(bound.max) // 99

๋ฐ˜ํ™˜๊ฐ’์— ๋Œ€ํ•œ ์ ‘๊ทผ๋„ bound.min ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜ํ™˜์ด ์˜ต์…”๋„์ธ ๊ฒฝ์šฐ

๋‹น์—ฐํžˆ ๋ฐ˜ํ™˜๊ฐ’์— ์ ‘๊ทผํ• ๋•Œ๋Š” ์˜ต์…”๋„์ฒด์ธ์ด๋‚˜ unwrapping์ด ํ•„์š”ํ•˜๋‹ค.

func minMax(array: [Int]) -> (min:Int, max:Int)?{
  if array.isEmpty {return nil}
  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)
}

if let bounds = minMax(array: [1,3,32,-9,49,9]){
    print(bounds.min)
}

ํ•จ์ˆ˜์˜ ์ธ์ž ๋ผ๋ฒจ ์ง€์ •ํ•˜๊ธฐ

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.

์œ„ ์ฝ”๋“œ์—์„œ๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ์—๋Š” from์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ , ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” homtown์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋‹ค.

์ธ์ž ์ƒ๋žต

ํŒŒ๋ผ๋ฏธํ„ฐ ์•ž์— ์–ธ๋”๋ฐ”(_)๋ฅผ ๋ถ™์ด๋ฉด ํ˜ธ์ถœ์‹œ์— ์ธ์ž๊ฐ’์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

func someFunction(_ firstParameterName: String, secondparameterName: String){

}
someFunction("์ธ์ž ๋ผ๋ฒจ ์•ˆ์จ๋„๋จ", secondparameterName: "secondName")

๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์„ค์ •

ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜๋ฉด , ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ์— ์ธ์ž๊ฐ’์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์ƒ๋žต๋˜๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ธฐ๋ณธ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
    // ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ ๋‘๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ƒ๋žตํ•˜๋ฉด ํ•จ์ˆ˜์•ˆ์—์„œ
    // parameterWithDefault๊ฐ’์€ 12๊ฐ€ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault๋Š” 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefault๋Š” 12

๊ฐ€๋ณ€์ธ์ž(Variadic Parameter)

...๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ณด๋‹ค ๋” ๋งŽ์€ ์ˆ˜์˜ ์ธ์ž๊ฐ’์„ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

func average(_ numbers: Int...) -> Double{
  var sum : Int = 0
  for number in numbers{
    sum += number
  }
  return Double(sum) / Double(numbers.count)
}
print(average(10,20,25,28,3))
  >>> 17.2
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.