Kotlin學習筆記(12) 定義函數
帶有兩個
Int
參數、返回Int
的函數:
帶有兩個
Int
參數、返回Double的函數:fun main() {
print("sum of 3 and 5 is ")
println(sum1(3, 5))
println("sum of 19 and 23 is ${sum2(19, 23)}")
printdiff(32.0 , 45.9 )
}
fun sum1(a: Int, b: Int): Double
{
return a + b +0.0
}
fun sum2(a: Int, b: Int) =a + b
fun printdiff(a: Double, b: Double): Unit {
println("difference of $a and $b is ${a - b}")
}
輸出畫面
sum of 3 and 5 is 8.0
sum of 19 and 23 is 42
difference of 32.0 and 45.9 is -13.899999999999999
沒有留言:
張貼留言