728x90
package com.example.kotlin
import androidx.compose.runtime.produceState
fun main() {
println("Hello world!!")
val result = test(1,c=5)
test2(name="박해세", id="hesse", nickname = "해태")
println(result)
println(time1(1,3))
println(time2(1,3))
}
// 2. 함수
fun test(a: Int, b: Int = 3, c:Int=4,) : Int { //이렇게 트레일링 콤마 붙여도 오류 안 남
println(a+b)
return a+b
}
//Unit이 void랑 같은 역할. 생략 가능. 모든 게 다 리턴 있다.
//fun test(a: Int): Int {
// val b = 3
// println(a+b)
// return a+b
//}
//오버로딩 b는 항상 값 정해져 있다. 이거 코틀린에서는 안 씀
//매개변수 뒤에 바로 디폴트 값 줄 수 있음. (코드의 간결성)
fun test2(name: String, nickname : String, id: String) = println(name + nickname + id)
fun time1(a:Int, b: Int) : Int {
return a * b
}
fun time2(a:Int,b: Int) = a*b //Single Expression
//정리
//함수는 fun으로 시작하고 함수명 나옴
//모든 함수 리턴 값 있는데 없으면 Unit 생략 돼있는 것 (자바 void)
// 매개변수 여러 개 들어갈 수 있고, 변수명: Type 이렇게 나오고 디폴트 값 함수에서 오버로딩 편하게 가능
// 한 줄로 코드 간결하게 SingleExpression 가능하다
// Trailing Comma 들어가도 오류가 나지 않는다.
// main에서 함수 매개변수 값 지정 가능728x90
'코틀린(Kotlin)' 카테고리의 다른 글
| (코틀린/Kotlin) 컬렉션 (list, map) (0) | 2024.02.06 |
|---|---|
| (코틀린/Kotlin) 반복문 (0) | 2024.02.06 |
| (코틀린/Kotlin) 조건식 (0) | 2024.02.06 |
| (코틀린/Kotlin) 클래스 (0) | 2024.02.06 |
| (코틀린/Kotlin) 변수 (0) | 2024.02.05 |