Functions are self-contained chunks of code that perform a specific task μΌλ ¨μ μμ μ μννλ μ½λ λ¬Άμμ μλ³ν μ μλ νΉμ ν μ΄λ¦μ λΆμ¬νμ¬ μ¬μ©νλ κ² μ ν
- Input κ³Ό Output μ΄ λͺ¨λ μλ κ² (Function)
- Input μ΄ μκ³ Output λ§ μλ κ² (Generator)
- Input μ΄ μκ³ Output μ μλ κ² (Consumer)
- Input κ³Ό Output μ΄ λͺ¨λ μλ κ²
func ν¨μμ΄λ¦(맀κ°λ³μ: νμ
) -> λ³κ²½ν νμ
{
κ²°κ³Όκ°/μΆλ ₯/리ν΄
}
λ°λ³΅λ¬Έ(for..in)μ μ΄μ©ν λ©μλ
func timesTable(num: Int) {
for i in 1...3 {
print("\(num) * \(i) = \(num * i)")
}
}
timesTable(num: 4)
timesTable(num: 3)
timesTable(num: 2)
timesTable(num: 1)
func hello1() {
print("Hello, world!")
}
hello1() // ν¨μλ₯Ό νΈμΆν μ½λκ° λλ €λ°λ κ°μ΄ μμ print
func hello2() -> String {
return "Hello, world!"
}
hello2() // ν¨μλ₯Ό νΈμΆν μ½λκ° String νμ
μ κ°μ λλ €λ°μ retu
voidμ νκΈ° (λͺ¨λ κ°μ μλ―Έ)
func say(number: Int) {
print(number)
}
func say(word: String) -> Void {//void (λΉμ΄μλ) μ무κ²λ λ°ννμ§ μλλ€. //μμ λμΌν μλ―Έ
print(word)
}
func say(something: String) -> () { // μμ λμΌν μλ―Έ
print(something)
}
say(number: 33)
say(word: "1")
say(something: "1")
func addNumbers(a: Int, b: Int) -> Int {
a + b
// return a + b
}
addNumbers(a: 10, b: 20) //30
addNumbers(a: 3, b: 5) //8
func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting //λ³μμ΄μ©
//λ³μμ΄μ©νμ§ μμΌλ©΄
// return "Hello, " + person + "!"
}
greet(person: "Anna")
greet(person: "Brian")
λλ½ν΄λ μ€νμ λλ€.
func addTwoNumbers(a: Int, b: Int) -> Int {
a + b
// return a + b //κ³Ό λμΌ
}
addTwoNumbers(a: 1, b: 2)
λ©μλ λ²μ
let outside = "outside"
//let inside = "inside" μ¬κΈ°λ‘ μμΌ μΆλ ₯λ¨
func scope() {
print(outside)
let inside = "inside" //λ²μ λ΄λΆμ μλ κ²μ μΆλ ₯μλ¨
print(inside)
}
//print(inside)
func someFunction(first: Int, second: Int) {
print(first, second)
}
someFunction(first: 1, second: 2)
/*
func ν¨μμ΄λ¦(맀κ°λ³μ1, 맀κ°λ³μ2 : νμ
) {}
*/
// Specifying Argument Labels (λͺ¨λμ°κΈ°)
func multiplyNumber(lhs num1: Int, rhs num2: Int) {
num1 + num2
}
multiplyNumber(lhs: 10, rhs: 10)
// Omitting Argument Labels (μλ΅κΈ°λ₯: _)
func someFunction(_ first: Int, second: Int) {
print(first, second)
}
//someFunction(first: 1, second: 2)
someFunction(1, second: 2) //_μ°κ² λλ©΄ μμ κ² μλ΅κ°λ₯
func someFunction(_ first: Int, _ second: Int) {
print(first, second)
}
//someFunction(first: 1, second: 2)
someFunction(1, 2) //_λκ° λ€ μλ΅κ°λ₯
* Argument Labelμ λ³λλ‘ μ§μ ν΄μΌνλ κ²½μ°
```swift
// argumentLabel μ§μ μμ
func use(item: String) {
print(item)
}
use(item: "Macbook")
//parameterName
func speak(to name: String) {
print(name) //λ΄λΆμμλ λ€μ
}
speak(to: "Tory") //νΈμΆν λλ to
- μ΄λ¦μ μ λ ₯ κ°μΌλ‘ λ°μμ μΆλ ₯νλ ν¨μ (κΈ°λ³Έ νν)
- λμ΄λ₯Ό μ λ ₯ κ°μΌλ‘ λ°μμ μΆλ ₯νλ ν¨μ (Argument Label μλ΅)
- μ΄λ¦μ μ λ ₯ κ°μΌλ‘ λ°μ μΈμ¬λ§μ μΆλ ₯νλ ν¨μ (Argument Label μ§μ )
func printName(inputName: String) {
print(inputName)
}
printName(inputName: "νκΈΈλ")
func printAge(inputAge: Int){
print(inputAge)
}
printAge(inputAge: 29)
func sayToHello(to inputName: String){
print("Hi! How are you? \(inputName)?")
}
sayToHello(to: "μμ")
(λΆμ΄ν)μλμ κ°μ μ§μ ν κ²½μ° μλ λμνμ§ μμ
func functionWithDefault(param: Int = 12) -> Int {
return param
}
functionWithDefault(param: 6)
// param is 6
functionWithDefault()
// param is 12
κ°λ³μΈμ νλΌλ―Έν°
//νκ· κ΅¬νκΈ°(Intλ²μ )
func average(num1: Int, num2: Int){
}
average(num1: 1, num2: 2)
//average(num1: 1, num2: 2, num3: 3)
//νκ· κ΅¬νκΈ°(Doubleμ΄μ©)
func arithmeticAverage(_ numbers: Double...) -> Double {//...μ μ¬λ¬κ°μ κ°μ λ°μ μ μλ€λ κ²
var total = 0.0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticAverage(1, 2, 3)
arithmeticAverage(1, 2, 3, 4, 5)
arithmeticAverage(3, 8.25, 18.75)
//print(10,20,30,40,50)
//print(1,2,3,4,5,6,7)
//μ€λ₯μ μμΈ? (μλμ°Έμ‘°)
//func arithmeticAverage2(_ numbers: Double..., _ last: Double) {
// print(numbers)
// print(last)
//}
//
//arithmeticAverage2(1, 2, 3,5)
//λκ°μ λ³μ νμμ and μ΄μ©
func arithmeticAverage3(_ numbers: Double..., and last: Double) { (andλ₯Ό μ΄μ©)
print(numbers)
print(last)
}
arithmeticAverage3(1, 2, 3, and: 5)
μΈλΆμλ μ¨κΈ°κ³ ν¨μ λ΄λΆμμλ§ μ¬μ©ν ν¨μλ₯Ό μ€μ²©νμ¬ μ¬μ©κ°λ₯
func plusFunction(input: Int) -> Int { input + 1 }
func minusFunction(input: Int) -> Int { input - 1 }
if plus {
return plusFunction(input: value)
} else {
return minusFunction(input: value)
}
}
var value = 4
chooseFunction(plus: true, value: value)
chooseFunction(plus: false, value: value)
func print(name: String) {
print(name)
}
print(name: "Tory")
func printAge(_ age: Int) {
print(age)
}
printAge(4)
func sayHello(to name: String) {
print(name)
}
sayHello(to: "Lilly")