๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2023 ์•ˆ๋“œ๋กœ์ด๋“œ/์•ˆ๋“œ๋กœ์ด๋“œ ์Šคํ„ฐ๋””

Kotlin์—์„œ ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ ๋ฐ ์‚ฌ์šฉํ•˜๊ธฐ

by hyeonha 2022. 11. 14.

๋ชฉ์ฐจ

    mainํ•จ์ˆ˜๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์‹œ์ž‘์ ์œผ๋กœ ์‚ฌ์šฉ
    ํ•จ์ˆ˜ ์ •์˜
    fun ํ•จ์ˆ˜๋ช…() {
    }
    ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ

    1)Unit ๋ฐ˜ํ™˜ ์œ ํ˜•
    ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

    2) String์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์œ ํ˜•
    ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋Š” ์˜ค์ง ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋งŒ ๋ฐ˜ํ™˜ ๊ฐ€๋Šฅ ๋ ค

    fun main() {
        println(birthdayGreeting())
    }
    
    fun birthdayGreeting(): String {
        val nameGreeting = "Happy Birthday, Rover!"
        val ageGreeting = "You are now 5 years old"
        return "$nameGreeting\n$ageGreeting"
    }

    birthdayGreeting ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ

    ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ณ€์ˆ˜์ด๋ฆ„๊ณผ ๋ฐ์ดํ„ฐ ์œ ํ˜•์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, :(์ฝœ๋ก )๊ณผ ๊ณต๋ฐฑ์„ ๊ตฌ๋ถ„๋œ๋‹ค.
    ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ๋‹ค.

    birthdayGreeting()ํ•จ์ˆ˜์— ์›ํ•˜๋Š” ์ด๋ฆ„์„ ์ „๋‹ฌํ•˜์—ฌ ์ƒ์ผ์„ ์ถ•ํ•˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž

    fun main() {
        println(birthdayGreeting("Rover"))
            println(birthdayGreeting("Rex"))
    }
    
    fun birthdayGreeting(name: String): String {
        val nameGreeting = "Happy Birthday, $name!"
        val ageGreeting = "You are now 5 years old"
        return "$nameGreeting\n$ageGreeting"
    }

    ๋งค๊ฐœ๋ณ€์ˆ˜ vs ์ธ์ˆ˜
    ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ๋Š” ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค.
    ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์œ„ํ•œ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
    ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์—‘์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜ (name ๋ณ€์ˆ˜)
    ์ธ์ˆ˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์‹ค์ œ๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ’("Rover" ๋ฌธ์ž์—ด)

    ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝ๋ถˆ๊ฐ€
    ํ•จ์ˆ˜ ๋ณธ๋ฌธ ์•ˆ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋‹ค๋ฅธ ๊ฐ’์„ ํ• ๋‹น x

    ์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜
    ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์— ๋‘˜ ์ด์ƒ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ใ…‡
    ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์œ ํ˜•์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ •์˜ ใ…‡

    fun main() {
        println(birthdayGreeting("Rover", 5))
            println(birthdayGreeting("Rex", 2))
    }
    
    fun birthdayGreeting(name: String, age: Int): String {
        val nameGreeting = "Happy Birthday, $name!"
        val ageGreeting = "You are now $age years old"
        return "$nameGreeting\n$ageGreeting"
    }

    ํ•จ์ˆ˜ ์„œ๋ช…
    ํ•จ์ˆ˜์˜ ์ด๋ฆ„, ์ž…๋ ฅ, ์ถœ๋ ฅ์„ ํ†ต์นญํ•˜๋ฉฐ ํ•จ์ˆ˜ ์„œ๋ช…์ด๋ผ๊ณ  ํ•œ๋‹ค.
    ์ค‘๊ด„ํ˜ธ ์ง์ „๊นŒ์ง€์˜ ๋ชจ๋“  ๋‚ด์šฉ์„ ํฌํ•จํ•œ๋‹ค.

    fun birthdayGreeting(name: String, age: Int): String

    ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„๋œ ๋งค๊ฐœ๋ณ€์ˆ˜ : ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก

    ์ด๋ฆ„์ด ์ง€์ •๋œ ์ธ์ˆ˜
    ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ or ์ธ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ์ „๋‹ฌํ•˜๋ ค๋Š” ๊ฒฝ์šฐ
    -> ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    fun main() {
        println(birthdayGreeting(name = "Rover", age = 5))
            println(birthdayGreeting(age = 2, name = ))
    }
    
    fun birthdayGreeting(name: String, age: Int): String {
        val nameGreeting = "Happy Birthday, $name!"
        val ageGreeting = "You are now $age years old"
        return "$nameGreeting\n$ageGreeting"
    }

    ๊ธฐ๋ณธ๊ฐ’ ์ธ์ˆ˜
    ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š” ์ธ์ˆ˜๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ, ์ƒ๋žตํ•˜๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์ด ์‚ฌ์šฉ๋œ๋‹ค

    fun main() {
        println(birthdayGreeting(age = 5))
            println(birthdayGreeting("Rex", 2))
    }
    
    fun birthdayGreeting(name: String = "Rover", age: Int): String {
        val nameGreeting = "Happy Birthday, $name !"
        val ageGreeting = "You are now $age years old"
        return "$nameGreeting\n$ageGreeting"
    }
    fun main() {
        println(birthdayGreeting(age = 5))
            println(birthdayGreeting(age = 2))
    }
    
    fun birthdayGreeting(name: String = "Rover", age: Int): String {
        val nameGreeting = "Happy Birthday, $name !"
        val ageGreeting = "You are now $age years old"
        return "$nameGreeting\n$ageGreeting"
    }
    728x90