ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] ν•¨μˆ˜(Func) - κ³ κΈ‰
    iOS/Swift 2022. 7. 25. 17:34
    πŸ’‘λ§€κ°œλ³€μˆ˜ κΈ°λ³Έκ°’

    기본값을 κ°–λŠ” λ§€κ°œλ³€μˆ˜λŠ” λ§€κ°œλ³€μˆ˜ λͺ©λ‘ 쀑에 λ’€μͺ½μ— μœ„μΉ˜ν•˜λŠ” 것이 μ’‹λ‹€

    λ§€κ°œλ³€μˆ˜ 기본값을 κ°€μ§€λŠ” λ§€κ°œλ³€μˆ˜λŠ” ν•¨μˆ˜ 호좜 μ‹œ μƒλž΅ν•  수 μžˆλ‹€

     

     func ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜μ΄λ¦„1: λ§€κ°œλ³€μˆ˜νƒ€μž…1, λ§€κ°œλ³€μˆ˜μ΄λ¦„2: λ§€κ°œλ³€μˆ˜νƒ€μž…2 = λ§€κ°œλ³€μˆ˜ κΈ°λ³Έκ°’...) -> λ°˜ν™˜νƒ€μž… {
        ν•¨μˆ˜ κ΅¬ν˜„λΆ€
        return λ°˜ν™˜κ°’
     }
    
    
    func greeting(friend: String, me: String = "yeonsu") {
        print ("Hello \(friend)! I'm \(me)")
    }
    greeting(friend: "John")    //Hello John! I'm yeonsu
    greeting(friend: "John", me: "bonny")

     

     

    πŸ’‘μ „λ‹¬μΈμž λ ˆμ΄λΈ”

    μ „λ‹¬μΈμž λ ˆμ΄λΈ”μ€ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ λ§€κ°œλ³€μˆ˜μ˜ 역할을 μ’€ 더 λͺ…ν™•ν•˜κ²Œ ν•˜κ±°λ‚˜ ν•¨μˆ˜ μ‚¬μš©μžμ˜ μž…μž₯μ—μ„œ ν‘œν˜„ν•˜κ³ μž ν•  λ•Œ μ‚¬μš©ν•œλ‹€

    (*ν•¨μˆ˜ 쀑볡 μ •μ˜ μ‹œμ— 유용)

     

    μ£Όμ˜ν•  점

    ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ „λ‹¬μΈμžλ₯Ό μ‚¬μš©ν•  λ•Œμ—λŠ” λ§€κ°œλ³€μˆ˜ 이름을 μ‚¬μš©ν•œλ‹€

    func greeting(to friend: String, from me: String) {

        print("Hello \(friend)! I'm \(me)")

    }

     

    ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œμ—λŠ” μ „λ‹¬μΈμž λ ˆμ΄λΈ”μ„ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€

    greeting(to: "gayeon", from: "yeonsu")

     func ν•¨μˆ˜μ΄λ¦„(μ „λ‹¬μΈμžλ ˆμ΄λΈ” λ§€κ°œλ³€μˆ˜μ΄λ¦„1: λ§€κ°œλ³€μˆ˜νƒ€μž…1, μ „λ‹¬μΈμžλ ˆμ΄λΈ” λ§€κ°œλ³€μˆ˜μ΄λ¦„2: λ§€κ°œλ³€μˆ˜νƒ€μž…2...) -> λ°˜ν™˜νƒ€μž… {
        ν•¨μˆ˜ κ΅¬ν˜„λΆ€
        return
     }
    
    
    // κΈ°μ‘΄ greeting ν•¨μˆ˜μ™€λŠ” 달리 greeting, to, from κΉŒμ§€ ν•¨μˆ˜μ˜ μ΄λ¦„μœΌλ‘œ μ·¨κΈ‰
    
    // ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ „λ‹¬μΈμžλ₯Ό μ‚¬μš©ν•  λ•Œμ—λŠ” λ§€κ°œλ³€μˆ˜ 이름을 μ‚¬μš©ν•œλ‹€
    func greeting(to friend: String, from me: String) {
        print("Hello \(friend)! I'm \(me)")
    }
    
    // ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œμ—λŠ” μ „λ‹¬μΈμž λ ˆμ΄λΈ”μ„ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€
    greeting(to: "gayeon", from: "yeonsu")

     

     

    πŸ’‘κ°€λ³€ λ§€κ°œλ³€μˆ˜

    전달 받을 κ°’μ˜ 개수λ₯Ό μ•ŒκΈ° μ–΄λ €μšΈ λ•Œ μ‚¬μš©ν•  수 μžˆλ‹€

    κ°€λ³€ λ§€κ°œλ³€μˆ˜λŠ” ν•¨μˆ˜ λ‹Ή ν•˜λ‚˜λ§Œ κ°€μ§ˆ 수 μžˆλ‹€

     

    func sayHelloToFriend(me: String, friend: String...) -> String {
        return "Hello \(friend)! I'm \(me)!"
    }
    
    print(sayHelloToFriend(me: "yeonsu", friend: "gayeon", "ato", "stranger"))
    
    print(sayHelloToFriend(me: "yeonsu"))   //Hello []! I'm yeonsu!

     

     

    πŸ’‘λ°μ΄ν„° νƒ€μž…μœΌλ‘œμ„œμ˜ ν•¨μˆ˜

    μŠ€μœ„ν”„νŠΈλŠ” ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° νŒ¨λŸ¬λ‹€μž„μ„ ν¬ν•¨ν•˜λŠ” 닀쀑 νŒ¨λŸ¬λ‹€μž„ 언어이닀

    μŠ€μœ„ν”„νŠΈμ˜ ν•¨μˆ˜λŠ” 일급 κ°μ²΄μ΄λ―€λ‘œ λ³€μˆ˜, μƒμˆ˜ 등에 μ €μž₯이 κ°€λŠ₯ν•˜κ³ , λ§€κ°œλ³€μˆ˜λ₯Ό 톡해 전달할 수 μžˆλ‹€

     

    // =================== < ν•¨μˆ˜μ˜ νƒ€μž…ν‘œν˜„ > ===================
    
    // λ°˜ν™˜νƒ€μž…μ„ μƒλž΅ν•  수 μ—†λ‹€
    // (λ§€κ°œλ³€μˆ˜νƒ€μž…1, λ§€κ°œλ³€μˆ˜νƒ€μž…2...) -> λ°˜ν™˜νƒ€μž…
    
    var someFunction: (String, String) -> Void = greeting(to: from:)
    someFunction("gayeon", "yeonsu")
    
    someFunction = greeting(friend: me:)
    someFunction("John", "yeonsu")
    
    // νƒ€μž…μ΄ λ‹€λ₯Έ ν•¨μˆ˜λŠ” ν• λ‹Ήν•  수 μ—†λ‹€
    // someFunction = sayHelloToFriend(me: friend:) //κ°€λ³€ λ§€κ°œλ³€μˆ˜λ₯Ό 가지기 λ•Œλ¬Έμ— 일반 λ§€κ°œλ³€μˆ˜μ™€ 닀름
    
    
    //function은 λ°˜ν™˜κ°’μ΄ μ—†κ³  String νƒ€μž… λ§€κ°œλ³€μˆ˜ 2개λ₯Ό κ°–λŠ” λ§€κ°œλ³€μˆ˜μ΄λ‹€
    func runAnother(function: (String, String) -> Void) {
        function("jenny", "john")
    }
    
    runAnother(function: greeting(friend: me:))
    runAnother(function: someFunction)

     

    전체 μ†ŒμŠ€μ½”λ“œ

    //
    //  Function2.swift
    //  iOS study
    //
    //  Created by yeonsu on 2022/07/25.
    //
    
    import Swift
    
    // =================== < λ§€κ°œλ³€μˆ˜ κΈ°λ³Έκ°’ > ===================
    // 기본값을 κ°–λŠ” λ§€κ°œλ³€μˆ˜λŠ” λ§€κ°œλ³€μˆ˜ λͺ©λ‘ 쀑에 λ’€μͺ½μ— μœ„μΉ˜ν•˜λŠ” 것이 μ’‹λ‹€
    
    /*
     
     func ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜μ΄λ¦„1: λ§€κ°œλ³€μˆ˜νƒ€μž…1, λ§€κ°œλ³€μˆ˜μ΄λ¦„2: λ§€κ°œλ³€μˆ˜νƒ€μž…2 = λ§€κ°œλ³€μˆ˜ κΈ°λ³Έκ°’...) -> λ°˜ν™˜νƒ€μž… {
        ν•¨μˆ˜ κ΅¬ν˜„λΆ€
        return λ°˜ν™˜κ°’
     }
     
     */
    
    func greeting(friend: String, me: String = "yeonsu") {
        print ("Hello \(friend)! I'm \(me)")
    }
    
    // λ§€κ°œλ³€μˆ˜ 기본값을 κ°€μ§€λŠ” λ§€κ°œλ³€μˆ˜λŠ” ν•¨μˆ˜ 호좜 μ‹œ μƒλž΅ν•  수 μžˆλ‹€
    greeting(friend: "John")    //Hello John! I'm yeonsu
    greeting(friend: "John", me: "bonny")
    
    
    
    // =================== < μ „λ‹¬μΈμž λ ˆμ΄λΈ” > ===================
    // μ „λ‹¬μΈμž λ ˆμ΄λΈ”μ€ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ λ§€κ°œλ³€μˆ˜μ˜ 역할을 μ’€ 더 λͺ…ν™•ν•˜κ²Œ ν•˜κ±°λ‚˜ ν•¨μˆ˜ μ‚¬μš©μžμ˜ μž…μž₯μ—μ„œ ν‘œν˜„ν•˜κ³ μž ν•  λ•Œ μ‚¬μš©ν•œλ‹€(*ν•¨μˆ˜ 쀑볡 μ •μ˜ μ‹œμ— 유용)
    
    /*
     
     func ν•¨μˆ˜μ΄λ¦„(μ „λ‹¬μΈμžλ ˆμ΄λΈ” λ§€κ°œλ³€μˆ˜μ΄λ¦„1: λ§€κ°œλ³€μˆ˜νƒ€μž…1, μ „λ‹¬μΈμžλ ˆμ΄λΈ” λ§€κ°œλ³€μˆ˜μ΄λ¦„2: λ§€κ°œλ³€μˆ˜νƒ€μž…2...) -> λ°˜ν™˜νƒ€μž… {
        ν•¨μˆ˜ κ΅¬ν˜„λΆ€
        return
     }
     
     */
    
    // κΈ°μ‘΄ greeting ν•¨μˆ˜μ™€λŠ” 달리 greeting, to, from κΉŒμ§€ ν•¨μˆ˜μ˜ μ΄λ¦„μœΌλ‘œ μ·¨κΈ‰
    
    // ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μ „λ‹¬μΈμžλ₯Ό μ‚¬μš©ν•  λ•Œμ—λŠ” λ§€κ°œλ³€μˆ˜ 이름을 μ‚¬μš©ν•œλ‹€
    func greeting(to friend: String, from me: String) {
        print("Hello \(friend)! I'm \(me)")
    }
    
    // ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œμ—λŠ” μ „λ‹¬μΈμž λ ˆμ΄λΈ”μ„ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€
    greeting(to: "gayeon", from: "yeonsu")
    
    
    
    
    // =================== < κ°€λ³€ λ§€κ°œλ³€μˆ˜ > ===================
    // 전달 받을 κ°’μ˜ 개수λ₯Ό μ•ŒκΈ° μ–΄λ €μšΈ λ•Œ μ‚¬μš©ν•  수 μžˆλ‹€
    // κ°€λ³€ λ§€κ°œλ³€μˆ˜λŠ” ν•¨μˆ˜ λ‹Ή ν•˜λ‚˜λ§Œ κ°€μ§ˆ 수 μžˆλ‹€
    
    /*
     
     func ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜μ΄λ¦„1: λ§€κ°œλ³€μˆ˜νƒ€μž…1, λ§€κ°œλ³€μˆ˜μ΄λ¦„2: λ§€κ°œλ³€μˆ˜νƒ€μž…2...) -> λ°˜ν™˜νƒ€μž… {
        ν•¨μˆ˜ κ΅¬ν˜„λΆ€
        return
     }
     
     */
    
    func sayHelloToFriend(me: String, friend: String...) -> String {
        return "Hello \(friend)! I'm \(me)!"
    }
    
    print(sayHelloToFriend(me: "yeonsu", friend: "gayeon", "ato", "stranger"))
    
    print(sayHelloToFriend(me: "yeonsu"))   //Hello []! I'm yeonsu!
    
    
    
    
    // =================== < 데이터 νƒ€μž…μœΌλ‘œμ„œμ˜ ν•¨μˆ˜ > ===================
    
    // μŠ€μœ„ν”„νŠΈλŠ” ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° νŒ¨λŸ¬λ‹€μž„μ„ ν¬ν•¨ν•˜λŠ” 닀쀑 νŒ¨λŸ¬λ‹€μž„ 언어이닀
    // μŠ€μœ„ν”„νŠΈμ˜ ν•¨μˆ˜λŠ” 일급 κ°μ²΄μ΄λ―€λ‘œ λ³€μˆ˜, μƒμˆ˜ 등에 μ €μž₯이 κ°€λŠ₯ν•˜κ³ , λ§€κ°œλ³€μˆ˜λ₯Ό 톡해 전달할 수 μžˆλ‹€
    
    
    
    // =================== < ν•¨μˆ˜μ˜ νƒ€μž…ν‘œν˜„ > ===================
    
    // λ°˜ν™˜νƒ€μž…μ„ μƒλž΅ν•  수 μ—†λ‹€
    // (λ§€κ°œλ³€μˆ˜νƒ€μž…1, λ§€κ°œλ³€μˆ˜νƒ€μž…2...) -> λ°˜ν™˜νƒ€μž…
    
    var someFunction: (String, String) -> Void = greeting(to: from:)
    someFunction("gayeon", "yeonsu")
    
    someFunction = greeting(friend: me:)
    someFunction("John", "yeonsu")
    
    // νƒ€μž…μ΄ λ‹€λ₯Έ ν•¨μˆ˜λŠ” ν• λ‹Ήν•  수 μ—†λ‹€
    // someFunction = sayHelloToFriend(me: friend:) //κ°€λ³€ λ§€κ°œλ³€μˆ˜λ₯Ό 가지기 λ•Œλ¬Έμ— 일반 λ§€κ°œλ³€μˆ˜μ™€ 닀름
    
    
    //function은 λ°˜ν™˜κ°’μ΄ μ—†κ³  String νƒ€μž… λ§€κ°œλ³€μˆ˜ 2개λ₯Ό κ°–λŠ” λ§€κ°œλ³€μˆ˜μ΄λ‹€
    func runAnother(function: (String, String) -> Void) {
        function("jenny", "john")
    }
    
    runAnother(function: greeting(friend: me:))
    runAnother(function: someFunction)

     

    좜λ ₯ κ²°κ³Ό

Designed by Tistory.