Kotlin之定义函数

java:

int add (int m ,int n){
  return m+n;
}
void process(int m){
  Systrm.out.println(m);
}

kotlin:

fun add(m:Int, n:Int):Int {
  return m + n
}
fun process(m:Int) {
  Systrm.out.println(m)
}

定义函数的三个部分:

  1.函数头 :kotlin中除了包含函数名之外,还必须包含fun关键字

  2.参数: 包含参数名和参数类型

  3.返回值:在kotlin中如果没有返回值,那么可以返回Unit 或者什么都不返回

原文地址:https://www.cnblogs.com/loaderman/p/7875995.html