iOS/๐ŸŸ  Swift

Nested Functions - Functions

woozzang 2020. 11. 8. 19:10

 

"ํ•จ์ˆ˜ ๋ธ”๋ก ๋‚ด๋ถ€์— ์ •์˜๋œ ํ•จ์ˆ˜"

 

Example Code

func outer() -> () -> () {
    print("outer")      // "outer"
    var a: Int = 3
    
    func inner(){
        print("inner")  // "inner"

        print(a)		// 3 -> Nested Function์˜ Scope : outer local block
    }
    return inner
}

let f = outer()

f()

 

 

outer ๋ธ”๋ก ์™ธ๋ถ€์—์„œ inner ์— ์ ‘๊ทผ ํ•˜๋Š” ๋ฐฉ๋ฒ•

  •   ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด inner๋ฅผ ๋ฆฌํ„ดํ•จ์œผ๋กœ์„œ ๊ฐ„์ ‘์  Scope ํ™•์žฅ์„ ํ†ตํ•ด inner์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๋.


 

์ด์ „ ๊ธ€: Function Types - Functions

 

Function Types - Functions

Function Types๋Š” ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ํ•จ์ˆ˜๋ฅผ assign ํ•˜๋Š” ๊ฒฝ์šฐ, ํƒ€์ž…์„ ๋ช…์‹œํ•  ๋•Œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. Swift์—์„œ ํ•จ์ˆ˜๋Š” first-class citizen ์ด๋ฏ€๋กœ, ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์— ํ•จ์ˆ˜๋ฅผ assign ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ๊ธ€์—์„œ Function No..

woozzang.tistory.com