๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Kotlin] Local Functions ๋กœ์ปฌ ํ•จ์ˆ˜

์ถœ์ฒ˜

https://kotlintutorial.io/local-functions-in-kotlin/

 

Local functions in Kotlin

In Kotlin there are local functions that can be defined within a larger function. These functions are only visible within this larger function and cannot be accessed from outside. Local functions have the advantage of making code more readable and clear by

kotlintutorial.io

ChatGPT

https://cornswrold.tistory.com/464

 

[Kotlin] ์ฝ”ํ‹€๋ฆฐ ๋กœ์ปฌํ•จ์ˆ˜์™€ ํ™•์žฅ

์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ํ•จ์ˆ˜์—์„œ ์ถ”์ถœํ•œ ํ•จ์ˆ˜๋ฅผ ์› ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ค‘์ฒฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฌธ๋ฒ•์ ์ธ ๋ถ€๊ฐ€ ๋น„์šฉ์„ ๋“ค์ด์ง€ ์•Š๊ณ  ๊น”๋”ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์กฐ์งํ•  ์ˆ˜ ์žˆ๋‹ค. (DRY. Don't Repeat Yourself) ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š”

cornswrold.tistory.com

https://realkotlin.com/tutorials/2018-06-12-local-functions-in-kotlin/

 

Local functions in Kotlin

Kotlin supports local functions, i.e. it allows you to put a function inside a function.

realkotlin.com

 


๋กœ์ปฌ ํ•จ์ˆ˜ Local Functions

ํ•จ์ˆ˜ ์•ˆ์— ์ •์˜๋œ ํ•จ์ˆ˜. ์™ธ๋ถ€ ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ๋ณผ ์ˆ˜ ์žˆ๊ณ , ๊ทธ ์™ธ๋ถ€์—์„œ๋Š” ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

๋กœ์ปฌ ํ•จ์ˆ˜์˜ ๋ชฉ์ 

1. ๋กœ์ง ์บก์Šํ™”

๋กœ์ปฌ ํ•จ์ˆ˜๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ž˜ ์ •์˜๋˜๊ณ  ํŠนํ™”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•จ์œผ๋กœ์จ ๋กœ์ง์„ ์บก์Šํ™”ํ•œ๋‹ค.

2. ๊ฐ€๋…์„ฑ

ํŠน์ • ๋ชฉ์  ๋‹ฌ์„ฑ์„ ์œ„ํ•ด ๋ช…ํ™•ํ•˜๊ฒŒ ์ •์˜๋œ ๋กœ์ปฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ํ–ฅ์ƒ๋œ๋‹ค.

3. ์žฌ์‚ฌ์šฉ์„ฑ

์ฝ”๋“œ ์ค‘๋ณต ์—†์ด ์™ธ๋ถ€ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์˜ค๋ฅ˜ ์†Œ์Šค๋ฅผ ์ค„์ด๊ณ  ๋” ์ผ๊ด€๋˜๊ณ  ์œ ์ง€๋ณด์ˆ˜ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค.

4. ํ•จ์ˆ˜ ์ฃผ์œ„์˜ ์ปจํ…์Šค์— ์ ‘๊ทผ

๋กœ์ปฌ ํ•จ์ˆ˜๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋กœ์ปฌ ํ•จ์ˆ˜์™€ ์ปจํ…์ŠคํŠธ ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์ด ๊ฐ€๋Šฅํ•ด์„œ ๋กœ์ปฌ ํ•จ์ˆ˜์— ์ถ”๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

๋กœ์ปฌ ํ•จ์ˆ˜๋Š” ๋ฉ”์„œ๋“œ ์ถ”์ถœ ๋ฆฌํŒฉํ† ๋ง์—์„œ๋ณด๋‹ค ๋” ํšจ์œจ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฉ”์„œ๋“œ ์ถ”์ถœ ๋ฆฌํŒฉํ† ๋ง์—์„œ ์ข…์ข… ๋ฐœ์ƒํ•˜๋Š” ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋กœ์ปฌ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ์ด๋‹ค. 

๋ฉ”์„œ๋“œ ์ถ”์ถœ ๋ฆฌํŒฉํ† ๋ง์„ ํ†ตํ•ด ์ฝ”๋“œ๋ฅผ ๋” ์ž‘์€ ๋‹จ์œ„๋กœ ๋‚˜๋ˆŒ ๋•Œ, ์™ธ๋ถ€ ํด๋ž˜์Šค๋‚˜ ๋‚ด๋ถ€ ํด๋ž˜์Šค, ํŒŒ์ผ๋กœ ํ•จ์ˆ˜๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š” ๋Œ€์‹  ํ•ด๋‹น ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๋กœ์ง์ด๋ผ๋ฉด ๋กœ์ปฌ ํ•จ์ˆ˜๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ๊น”๋”ํ•˜๊ณ  ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋งŽ์€ ๋ฉ”์„œ๋“œ๋ฅผ ํด๋ž˜์Šค๋‚˜ ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

์˜ˆ

https://cornswrold.tistory.com/464

 

[Kotlin] ์ฝ”ํ‹€๋ฆฐ ๋กœ์ปฌํ•จ์ˆ˜์™€ ํ™•์žฅ

์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ํ•จ์ˆ˜์—์„œ ์ถ”์ถœํ•œ ํ•จ์ˆ˜๋ฅผ ์› ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ค‘์ฒฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฌธ๋ฒ•์ ์ธ ๋ถ€๊ฐ€ ๋น„์šฉ์„ ๋“ค์ด์ง€ ์•Š๊ณ  ๊น”๋”ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์กฐ์งํ•  ์ˆ˜ ์žˆ๋‹ค. (DRY. Don't Repeat Yourself) ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š”

cornswrold.tistory.com

https://realkotlin.com/tutorials/2018-06-12-local-functions-in-kotlin/

 

Local functions in Kotlin

Kotlin supports local functions, i.e. it allows you to put a function inside a function.

realkotlin.com