ํจ์๋ ํน์ ๊ธฐ๋ฅ์ ์คํํ๊ธฐ ์ํ ์ฝ๋์ ๋ฌถ์์ด๋ค. ๋์์ ๋ฐ๋ผ ํจ์์ ํน์ ์ด๋ฆ์ ์ค์ ํ ์ ์์ผ๋ฉฐ ์์ ์ ์ํํ๊ธฐ ์ํด ํธ์ถํ ์ ์๋ค.
ํจ์ ์ ์์ ํธ์ถ (Defining and Calling Functions)
ํจ์๋ฅผ ์ ์ํ ๋ ์ ํ์ ์ผ๋ก ํ๋ผ๋ฏธํฐ์ ์ด๋ฆ์ ์ ํด์ฃผ์ด์ผํ๋ค. ๋ํ ๋ฆฌํด ๊ฐ๋ ์ ์ํ ์ ์๋ค. ๋ชจ๋ ํจ์๋ ํจ์์ ๊ธฐ๋ฅ์ ๋ํ๋ด๋ ๊ณ ์ ์ ์ด๋ฆ์ ๊ฐ์ง๊ณ ์๋ค. ํจ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ํธ์ถ์ ํด์ผํ๋ฉฐ ์๊ท๋จผํธ์ ๊ฐ์ ๋ฃ์ด ์ฃผ์ด์ผ ํ๋ค. ์๊ท๋จผํธ๋ ํ๋ผ๋ฏธํฐ์ ์์์ ํญ์ ๊ฐ์์ผ ํ๋ค.
ํจ์ ํ๋ผ๋ฏธํฐ์ ๋ฐํ๊ฐ (Function Parameters and Return Values)
Swift๋ ๋งค์ฐ ์ ์ฐํ ํ๋ผ๋ฏธํฐ์ ๋ฐํ๊ฐ์ ๊ฐ์ง๊ณ ์๋ค.
ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ํจ์ (Functions Without Parameters)
ํจ์๋ ํญ์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ํ์ง ์์๋ ๋๋ค. ์์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ ๋ ฅ ์ฝ๋๊ฐ ์๊ธฐ ๋๋ฌธ์ ํญ์ ๊ฐ์ String์ ๋ฐํํ๋ค.
func sayHelloWorld() -> String {
return "hello, world"
}
print(sayHelloWorld())
// Prints "hello, world"
์ฌ๋ฌ๊ฐ ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ํจ์ (Functions With Multiple Parameters)
ํจ์๋ ํจ์์ ์๊ดํธ ๋ด์ ์ฝค๋ง๋ก ๊ตฌ๋ถํ์ฌ ์ฌ๋ฌ๊ฐ์ ์ ๋ ฅ ํ๋ผ๋ฏธํฐ๋ฅผ ๊ฐ์ง ์ ์๋ค.
func greet(person: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return greetAgain(person: person)
} else {
return greet(person: person)
}
}
print(greet(person: "Tim", alreadyGreeted: true))
// Prints "Hello again, Tim!"
๋ฐํ๊ฐ ์๋ ํจ์ (Functions Without Return Values)
func greet(person: String) {
print("Hello, \(person)!")
}
greet(person: "Dave")
// Prints "Hello, Dave!"
NOTEโ๏ธ
์๋ฐํ ๋งํ๋ฉด greet(person:) ํจ์๋ ๋ฐํ๊ฐ์ ์ ์ํ์ง ์์์ง๋ง ์ฌ์ ํ ๋ฐํ๊ฐ์ด ์๋ค. ๋ฐํ ํ์ ์ด ์ ์๋์ง ์์ ํจ์๋ Void ํ์ ์ ํน๋ณํ ๊ฐ(๋นํํ())์ ๋ฐํํ๋ค.
์ฌ๋ฌ๊ฐ์ ๋ฐํ๊ฐ์ด ์๋ ํจ์ (Functions with Multiple Return Values)
์ฌ๋ฌ๊ฐ์ ๊ฐ์ ๋ฐํํ๊ธฐ ์ํด ํจ์์ ๋ฐํ ํ์ ์ผ๋ก ํํ ํ์ ์ ์ฌ์ฉํ ์ ์๋ค.
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)
}
ํจ์ ์ธ์ ๋ผ๋ฒจ๊ณผ ํ๋ผ๋ฏธํฐ ์ด๋ฆ (Function Argument Labels and Parameter Names)
ํจ์์ ํ๋ผ๋ฏธํฐ์ ๊ณ ์ ์ ์ด๋ฆ์ ๋ถ์ฌํ ์ ์๋ค.
์๊ท๋จผํธ ๋ผ๋ฒจ ์ง์ (Specifying Argument Labels)
๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ํ๋ผ๋ฏธํฐ ์ด๋ฆ ์์ ์๊ท๋จผํธ ๋ผ๋ฒจ์ ์์ฑํ๋ค.
func someFunction(argumentLabel parameterName: Int) {
// In the function body, parameterName refers to the argument value
// for that parameter.
}
func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill! Glad you could visit from Cupertino."
ํ๋ผ๋ฏธํฐ ๊ธฐ๋ณธ๊ฐ (Default Parameter Values)
ํ๋ผ๋ฏธํฐ์ ํ์ ๋ค์ ํ๋ผ๋ฏธํฐ ๊ฐ์ ํ ๋นํ์ฌ ํจ์์ ํ๋ผ๋ฏธํฐ์ ๊ธฐ๋ณธ๊ฐ (default value) ์ ์ ์ํ ์ ์๋ค.
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
// If you omit the second argument when calling this function, then
// the value of parameterWithDefault is 12 inside the function body.
}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault is 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12
๊ธฐ๋ณธ๊ฐ์ ๊ฐ์ง๊ณ ์์ง ์์ ํ๋ผ๋ฏธํฐ๊ฐ ๋์ฑ ์ค์ํ ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ํ๋ผ๋ฏธํฐ ๋ชฉ๋ก์ ์์ ์์นํด์ผ ํ๋ค
In-Out ํ๋ผ๋ฏธํฐ (In-Out Parameters)
ํจ์์ ํ๋ผ๋ฏธํฐ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์์์ด๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ํด๋น ํจ์์ ๋ฐ๋ ๋ด์์ ํจ์์ ํ๋ผ๋ฏธํฐ ๊ฐ ๋ณ๊ฒฝ์ ์๋ํ๋ฉด ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ์ด๋ ํ๋ผ๋ฏธํฐ์ ๊ฐ์ ๋ณ๊ฒฝํ๊ธฐ ์ํด์๋ in-out ํค์๋๊ฐ ํ์ํ๋ค. ํจ์์ ์์ ๊ฐ๋ฅํจ์ ์๋ฆฌ๊ธฐ ์ํด in-out ํ๋ผ๋ฏธํฐ ์๊ท๋จผํธ์ &๋ฅผ ๋ถ์ฌ์ค๋ค.
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"
'๐ฆ Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Enumerations (0) | 2022.06.15 |
---|---|
Closures (0) | 2022.06.15 |
Control Flow (0) | 2022.06.01 |
Collection Types (0) | 2022.05.22 |
Strings and Characters (0) | 2022.05.22 |