๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

์ดˆ๋ฐ˜์€  ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋Š” ์ง€์‹ ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ํŒจ์Šคํ•˜๊ณ   ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ์ง€์‹  ์œ„์ฃผ๋กœ ์ •๋ฆฌํ•ด ๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

 

Functions With an Implicit Return

func greeting(for person: String) -> String {
    "Hello, " + person + "!"
}
  • ์ „์ฒด ๋ณธ๋ฌธ์ด ํ•œ์ค„๋กœ ํ‘œํ˜„์ด ๋œ๋‹ค๋ฉด returnํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

 

Specifying Argument Labels

func greet(person: String, from hometown: String) -> String {
    return "Hello \\(person)!  Glad you could visit from \\(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
  • ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ธ์ž ์ด๋ฆ„ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๊ฐ€๋ณ€ ํŒŒ๋ผ๋ฏธํ„ฐ (Variadic Parameters)

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
  • 0๊ฐœ ์ด์ƒ์˜ ํŠน์ • ํƒ€์ž…์˜ ๊ฐ’์„ ํ—ˆ์šฉํ•œ๋‹ค.
  • ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ž…๋ ฅ๊ฐ’์ด ์ „๋‹ฌ๋  ์ˆ˜ ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์ผ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž… ์ด๋ฆ„ ๋’ค์— ์„ธ๊ฐœ์˜ ๊ฐ„๊ฒฉ ๋ฌธ์ž (...)๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

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)
  • ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์ˆ˜๋‹ค.
  • ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ณ  ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์ข…๋ฃŒ๋œ ํ›„์—๋„ ์ด๋Ÿฌํ•œ ๋ณ€๊ฒฝ๋œ ๊ฐ’์„ ์œ ์ง€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ, inout ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...