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



沒有留言:

張貼留言

  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...