[Kotlin ๊ธฐ์ดˆ] ์ฝ”ํ‹€๋ฆฐ ๊ธฐ๋ณธ ๋ฌธ๋ฒ• - ํ•จ์ˆ˜
๋ฐ˜์‘ํ˜•

 

 

 

 

 

6 ํ•จ์ˆ˜

 

 

  • ์ฝ”๋“œ์˜ ์‹คํ–‰์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์—์„œ ์‹œ์ž‘
    [ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘] -- ํ˜ธ์ถœ --> [ํ•จ์ˆ˜]
  • ๋‹จ์ˆœํžˆ ๋งํ•˜๋ฉด ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ๋„ onCreate() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹คํ–‰๋œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ

 

 

ํ•จ์ˆ˜์˜ ์ •์˜

fun ํ•จ์ˆ˜๋ช…(ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„: ํƒ€์ž…): ๋ฐ˜ํ™˜ ํƒ€์ž… {
    return ๊ฐ’
}
  • ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ๋‹จ์œ„
  • fun ํ‚ค์›Œ๋“œ๋กœ ์ •์˜ํ•˜๋ฉฐ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ ์‚ฌ์šฉ
  • ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” ํ•จ์ˆ˜๋Š” ๋‚ด๋ถ€์—์„œ return์œผ๋กœ ๊ฐ’์„ ๋ฐ˜ํ™˜
  • ํŒŒ๋ผ๋ฏธํ„ฐ(Parameter) = ํ•จ์ˆ˜์— ์ž…๋ ฅ๋  ๊ฐ’
  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ’์„ ๋„ฃ์–ด์„œ ์ „๋‹ฌํ•˜๋Š”๋ฐ, ์ด ๋•Œ ์ž…๋ ฅ๋˜๋Š” ๊ฐ’์˜ ํƒ€์ž…์€ ํ•จ์ˆ˜์— ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž…๋ ฅํ•ด์•ผ ํ•จ

 

 

1) ๋ฐ˜ํ™˜๊ฐ’๊ณผ ์ž…๋ ฅ๊ฐ’์ด ์žˆ๋Š” ํ•จ์ˆ˜์˜ ์ •์˜

fun square(x: Int): Int {
    return x * x
}
  • ์˜ˆ์‹œ๋Š” Int ํƒ€์ž…์˜ x๋ผ๋Š” ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜ square()
  • square ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ž…๋ ฅ๊ฐ’์„ ์ œ๊ณฑํ•ด์„œ ํ˜ธ์ถœํ•œ ์ธก์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‹คํ–‰ ์ข…๋ฃŒ

 

 

2) ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋Š” ํ•จ์ˆ˜์˜ ์ •์˜

fun printSum(x: Int, y: Int) {
    Log.d("fun", "x + y = ${x + y}")
}
  • ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„ printSum ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๊ฒฐ๊ดŽ๊ฐ’์„ Log๋กœ ์ถœ๋ ฅํ•˜๊ณ  ์‹คํ–‰ ์ข…๋ฃŒ
  • ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋‹ค๋ฉด fun printSum(x: Int, u: Int) : Unit {} ๊ณผ ๊ฐ™์ด Unit์„ ์จ์ค˜๋„ ๋จ

 

 

3) ์ž…๋ ฅ๊ฐ’ ์—†์ด ๋ฐ˜ํ™˜๊ฐ’๋งŒ ์žˆ๋Š” ํ•จ์ˆ˜์˜ ์ •์˜

fun getPi(): Double {
    return 3.14
}
  • ์˜ˆ์‹œ์—์„œ์˜ getPi ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœํ•œ ์ธก์œผ๋กœ 3.14๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‹คํ–‰ ์ข…๋ฃŒ

 

 

 

ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ

 

ํ•จ์ˆ˜๋ช…(๊ฐ’)
  • ํ•จ์ˆ˜๋ช… ๋’ค์— ๊ด„ํ˜ธ๋ฅผ ๋ถ™์—ฌ์„œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ (ํ•จ์ˆ˜๋ช…์œผ๋กœ๋งŒ ์‹คํ–‰ ์•ˆ ๋จ)
  • ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ํ•จ์ˆ˜์˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ๋งˆ์น˜ ๊ฐ’์ฒ˜๋Ÿผ ์ธ์‹๋จ

 

 

1) ๋ฐ˜ํ™˜๊ฐ’๊ณผ ์ž…๋ ฅ๊ฐ’์ด ์žˆ๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ

var squareResult = square(30)
Log.d("fun", "30์˜ ์ œ๊ณฑ์€ ${squareResult}์ž…๋‹ˆ๋‹ค.")
  • ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ „๋‹ฌํ•  ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  ๋ฐ˜ํ™˜๊ฐ’์€ ๋Œ€์ž… ์—ฐ์‚ฐ์ž(=)๋กœ ๋ณ€์ˆ˜์— ๋‹ด์€ ํ›„์— ์‚ฌ์šฉ

 

 

2) ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ

printSum(3, 5)
  • ๋ฐ˜ํ™˜๊ฐ’์ด ์—†์œผ๋ฉด ๋Œ€์ž… ์—ฐ์‚ฐ์ž(=)๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ํ˜ธ์ถœํ•ด์„œ ์‹คํ–‰

 

 

3) ์ž…๋ ฅ๊ฐ’์ด ์—†๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ

val PI = getPi()
Log.d("fun", "์ง€๋ฆ„์ด 10์ธ ์›์˜ ๋‘˜๋ ˆ๋Š” ${10 * PI}์ž…๋‹ˆ๋‹ค.")
  • ์ž…๋ ฅ๊ฐ’์ด ์—†๋‹ค๋ฉด ๊ด„ํ˜ธ๋งŒ์œผ๋กœ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

 

 

 

ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ •์˜

fun ํ•จ์ˆ˜๋ช…((val ์ƒ๋žต) name1: String, name2: Int, name3: Double) { ์‹คํ–‰ ์ฝ”๋“œ }
  • ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋งˆ์น˜ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•˜๋“ฏ์ด '์ด๋ฆ„: ํƒ€์ž…'์˜ ํ˜•ํƒœ๋กœ ์ •์˜
  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ •์˜๋  ๊ฒฝ์šฐ๋Š” ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„
  • Kotlin์—์„œ ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ๋˜๋Š” ๊ฐ’์€ ๋ชจ๋‘ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฐ’(immutable)
  • ๋”ฐ๋ผ์„œ Kotlin์—์„œ์˜ ํ•จ์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ชจ๋‘ ์ฝ๊ธฐ ์ „์šฉ ํ‚ค์›Œ๋“œ val์ด ์ƒ๋žต๋œ ํ˜•ํƒœ

 

 

 

ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ธฐ๋ณธ๊ฐ’ ์ •์˜์™€ ํ˜ธ์ถœ

fun newFunction(name: String, age: Int = 29, weight: Double = 65.5){
    Log.d("fun", "name์˜ ๊ฐ’์€ ${name}์ž…๋‹ˆ๋‹ค.")
    Log.d("fun", "age์˜ ๊ฐ’์€ ${age}์ž…๋‹ˆ๋‹ค.")
    Log.d("fun", "weight์˜ ๊ฐ’์€ ${weight}์ž…๋‹ˆ๋‹ค.")
}
โ€‹
newFunction("Hello")
โ€‹
/** [๋กœ๊ทธ์บฃ ์ถœ๋ ฅ ๋‚ด์šฉ]
name์˜ ๊ฐ’์€ Hello์ž…๋‹ˆ๋‹ค.
age์˜ ๊ฐ’์€ 29์ž…๋‹ˆ๋‹ค.
weight์˜ ๊ฐ’์€ 65.5์ž…๋‹ˆ๋‹ค.
*/
  • ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ •์˜ํ•  ๋•Œ ๋“ฑํ˜ธ(=)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
  • ์˜ˆ์‹œ์—์„œ ๊ธฐ๋ณธ๊ฐ’์ด ์—†๋Š” ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์—๋งŒ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด, ๋‘์„ธ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์—๋Š” ๊ธฐ๋ณธ๊ฐ’์ด ์ž๋™์œผ๋กœ ์ž…๋ ฅ๋จ

 

 

 

ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ธฐ

fun newFunction(name: String, age: Int = 29, weight: Double = 65.5){
    Log.d("fun", "name์˜ ๊ฐ’์€ ${name}์ž…๋‹ˆ๋‹ค.")
    Log.d("fun", "age์˜ ๊ฐ’์€ ${age}์ž…๋‹ˆ๋‹ค.")
    Log.d("fun", "weight์˜ ๊ฐ’์€ ${weight}์ž…๋‹ˆ๋‹ค.")

newFunction("Michael", weight = 67.5)
โ€‹
/** [๋กœ๊ทธ์บฃ ์ถœ๋ ฅ ๋‚ด์šฉ]
name์˜ ๊ฐ’์€ Michale์ž…๋‹ˆ๋‹ค.
age์˜ ๊ฐ’์€ 29์ž…๋‹ˆ๋‹ค.
weight์˜ ๊ฐ’์€ 67.5์ž…๋‹ˆ๋‹ค.
*/
  • ํ•จ์ˆ˜์— ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์˜ ์˜๋ฏธ๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์„ ๋•Œ๊ฐ€ ์žˆ์Œ
  • ์ด๋Ÿด ๋•Œ ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์„œ ์ง์ ‘ ๊ฐ’์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Œ

 

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T