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



沒有留言:

張貼留言

RFID TI 培訓影片系列

RFID TI 培訓影片系列  https://www.ti.com/zh-tw/video/series/rfid.html 培訓影片系列 RFID 隨著創新技術日益發展,RFID 和 RF 術語越來越容易讓人混淆。本訓練系列詳細介紹了使用案例、權衡技術優缺點,讓您清楚知道該選...