2019年5月21日 星期二

Kotlin學習筆記(12) 定義函數

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



沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...