Skip to content

Latest commit

Β 

History

History
313 lines (229 loc) Β· 5.81 KB

swiftBasice-3.md

File metadata and controls

313 lines (229 loc) Β· 5.81 KB

Swift Basice

Function

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)

Functions without parameters

func hello1() {
  print("Hello, world!")
}

hello1()   // ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œ μ½”λ“œκ°€ λŒλ €λ°›λŠ” 값이 μ—†μŒ print


func hello2() -> String {
  return "Hello, world!"
}

hello2()   // ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œ μ½”λ“œκ°€ String νƒ€μž…μ˜ 값을 λŒλ €λ°›μŒ retu

Functions without return values

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")

Functions with params and return values

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")

Omit Return

λˆ„λ½ν•΄λ„ 싀행은 λœλ‹€.

func addTwoNumbers(a: Int, b: Int) -> Int {
  a + b
//  return a + b   //κ³Ό 동일
}

addTwoNumbers(a: 1, b: 2)

Function Scope

λ©”μ†Œλ“œ λ²”μœ„

let outside = "outside"
//let inside = "inside" μ—¬κΈ°λ‘œ 와야 좜λ ₯됨

func scope() {
  print(outside)

    let inside = "inside" //λ²”μœ„ 내뢀에 μžˆλŠ” 것은 좜λ ₯μ•ˆλ¨
  print(inside)
}

//print(inside)

Argument Label

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

Question

  • 이름을 μž…λ ₯ κ°’μœΌλ‘œ λ°›μ•„μ„œ 좜λ ₯ν•˜λŠ” ν•¨μˆ˜ (κΈ°λ³Έ ν˜•νƒœ)
  • λ‚˜μ΄λ₯Ό μž…λ ₯ κ°’μœΌλ‘œ λ°›μ•„μ„œ 좜λ ₯ν•˜λŠ” ν•¨μˆ˜ (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: "μš”μš”")

Default Parameter Values

(λΆˆμ΄ν–‰)μ•„λž˜μ— 값을 μ§€μ •ν•œ 경우 μœ„λŠ” λ™μž‘ν•˜μ§€ μ•ŠμŒ

func functionWithDefault(param: Int = 12) -> Int {
  return param
}

functionWithDefault(param: 6)
// param is 6

functionWithDefault()
// param is 12

Variadic Parameters

κ°€λ³€μΈμž νŒŒλΌλ―Έν„°

//ν‰κ· κ΅¬ν•˜κΈ°(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)

Nested Functions

μ™ΈλΆ€μ—λŠ” 숨기고 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œλ§Œ μ‚¬μš©ν•  ν•¨μˆ˜λ₯Ό μ€‘μ²©ν•˜μ—¬ μ‚¬μš©κ°€λŠ₯

  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)
  }
}

Answer

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")