ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] Function Notation - Functions
    iOS/🟠 Swift 2020. 11. 8. 11:32

    " ν•¨μˆ˜ν‘œκΈ°λ²• "

    μ•ˆλ…•ν•˜μ„Έμš”. μ˜€λŠ˜μ€ ν•¨μˆ˜ ν‘œκΈ°λ²•μ— λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€. πŸ‘€

     

    μŠ€μœ„ν”„νŠΈμ—μ„œ ν•¨μˆ˜ ν‘œκΈ°λ²•μ€ 크게 두 가지 κ²½μš°μ— ν•„μš”ν•©λ‹ˆλ‹€.

     

        1. μ˜€λ²„λ‘œλ”© 된 ν•¨μˆ˜λ₯Ό κ΅¬λΆ„ν•˜μ—¬ λΆ€λ₯Ό ν•„μš”κ°€ μžˆμ„ λ•Œ

     

        2. μΌκΈ‰κ°μ²΄λ‘œμ„œ λ³€μˆ˜ ν˜Ήμ€ μƒμˆ˜μ— μ €μž₯ν•  λ•Œ

     


     

    ν•¨μˆ˜λ₯Ό ν‘œν˜„ν•˜λŠ” 기본적인 방법

    개발자 μ‚¬μ΄νŠΈ(ν…Œν¬λΈ”λ‘œκ·Έ)λ‚˜, λ ˆνΌλŸ°μŠ€μ—μ„œλŠ” ν•¨μˆ˜λ₯Ό μ•„λž˜μ™€ 같이 ν‘œκΈ°ν•©λ‹ˆλ‹€.

    /* Function Notation */
    
    random(in:)
    
    print(_:seperator:terminator:)

     

     

    1. μ˜€λ²„λ‘œλ”© 된 ν•¨μˆ˜λ₯Ό ꡬ뢄할 λ•Œ

     

    νŒŒλΌλ―Έν„°κ°€ μ—†λŠ” ν•¨μˆ˜μ˜ 경우 Function Name만 μ‚¬μš©ν•˜μ—¬ ν‘œκΈ°ν•©λ‹ˆλ‹€.

     

    그렇지 μ•Šμ€ 경우, Argument Label을 ' : ' 둜 κ΅¬λΆ„ν•˜μ—¬ μž…λ ₯ν•©λ‹ˆλ‹€.

     

    μ˜€λ²„λ‘œλ”© λ˜μ–΄μžˆλŠ” constraint λ©”μ†Œλ“œ

    μœ„ μ΄λ―Έμ§€μ—μ„œ XcodeλŠ” Argurment Lable κ³Ό ' : ' 을 λ‚˜μ—΄ν•˜μ—¬ μ˜€λ²„λ‘œλ”© λ˜μ–΄μžˆλŠ” λ©”μ†Œλ“œλ“€μ„ κ΅¬λΆ„ν•˜μ—¬ 보여주고 μžˆμŠ΅λ‹ˆλ‹€.


     

    2. μΌκΈ‰κ°μ²΄λ‘œμ„œ λ³€μˆ˜ ν˜Ήμ€ μƒμˆ˜μ— μ €μž₯ν•  λ•Œ

    Swiftμ—μ„œ ν•¨μˆ˜λŠ” first-class citizen μž…λ‹ˆλ‹€.

     

    μœ„μ™€ 같은 경우, ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄κ°€ first-class functionsλ₯Ό κ°€μ‘Œλ‹€κ³  λ§ν•©λ‹ˆλ‹€.

    In computer science, a promgramming language is said to have fisrt-class functions if it treats functions as first-class citizens.

     

    λ”°λΌμ„œ ν•¨μˆ˜λ₯Ό λ³€μˆ˜ ν˜Ήμ€ μƒμˆ˜μ— μ €μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

     

    μ΄λ•Œ, Function Notation을 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

     

    func add(first: Int, second: Int) -> Int {
    	return first + second
    }
    
    var cal = add(fisrt:second:)    // ν•¨μˆ˜ ν‘œκΈ°λ²•μ„ μ΄μš©ν•΄ λ³€μˆ˜μ— ν•¨μˆ˜λ₯Ό μ €μž₯
    
    cal(1,2)    // λ³€μˆ˜μ— μ €μž₯된 ν•¨μˆ˜λŠ” Argumnet Label 없이 ν˜ΈμΆœν•΄μ•Ό ν•©λ‹ˆλ‹€.

     

     

     

    끝!


     

    이전 κΈ€: Variadic Parameters - Functions

     

    πŸ€–πŸŸ’[우짱의 iOS λΈ”λ‘œκ·Έ]πŸ”΅πŸ’»

    iOSλ₯Ό κ³΅λΆ€ν•˜λ©΄μ„œ 배운 λ‚΄μš©μ„ κΈ°λ‘ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

     

    'iOS > 🟠 Swift' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

    Nested Functions - Functions  (0) 2020.11.08
    Function Types - Functions  (0) 2020.11.08
    Variadic Parameters - Functions  (0) 2020.11.07
    Argument Label - Functions  (0) 2020.11.07
    In-Out Parameters - Functions  (0) 2020.11.06

    λŒ“κΈ€

μ–΄μ œλ³΄λ‹€ λ°œμ „ν•œ λ‚˜