scala的基本函数调用

37 阅读2分钟

1定义函数

案例:找到x,y中的较大者

scss
 体验AI代码助手
 代码解读
复制代码
def max(x :Int, y : Int):Int={
  if(x>y)
    x
  else
    y
}

  def main(args: Array[String]): Unit = {
    var rst =max(2,3)
    println(s"23的较大者是:${rst}")
  }

2. 函数的基本使用

返回值:

1.指定返回类型

2.函数内,最后一个表达式(最后一句代码)就是函数的返回值

3.unit 表示这个函数没有返回值(void)

参数:

1.如果没有参数,在定义函数时,可以省略(),在调用函数的时候,不能添加()

(1)指定返回
scss
 体验AI代码助手
 代码解读
复制代码
def max(x :Int, y : Int):Int={
if(x>y)
  x
else {
  y
  100
}
}

def main(args: Array[String]): Unit = {
  var rst =max(2,3)
  println(s"23的较大者是:${rst}")
}
(2)函数内,最后一个表达式(最后一句代码)就是函数的返回值
scss
 体验AI代码助手
 代码解读
复制代码
def max(x :Int, y : Int):Int={
  if(x>y)
    x
  else {
    y
  }
}
  // 如果没有参数,可以直接省略
  def say():Unit={
    println("hello world")
  }
  def main(args: Array[String]): Unit = {
    var rst =max(2,3)
    println(s"23的较大者是:${rst}")
    say
  }

参数:如果没有参数,在定义函数时,可以省略(),在调用函数的时候,不能添加()

scss
 体验AI代码助手
 代码解读
复制代码
def max(x :Int, y : Int):Int={
  if(x>y)
    x
  else {
    y
  }
}
  // 如果没有参数,可以直接省略
  def say():Unit={
    println("hello world")
  }
  def main(args: Array[String]): Unit = {
    var rst =max(2,3)
    println(s"23的较大者是:${rst}")
    say()
  }

3.案例:比较x y z 中的较大者

(1) 三选一

kotlin
 体验AI代码助手
 代码解读
复制代码
object basic34 {

def max(x :Int, y : Int,z :Int):Int={
    if (x>=y && x>=z)
      x
     else if (y>=x && y>=z)
       y
      else
        z

}

  def main(args: Array[String]): Unit = {
    var rst =max(2,3,5)
    println(s"2和3的较大者是:${rst}")

  }
}
kotlin
 体验AI代码助手
 代码解读
复制代码
object basic34 {

def max(x :Int, y : Int,z :Int):Int={
  if(x>y)
    x
   else
     y
    val rst = if(x>y)  x
    else  y
  if (rst >z)
    rst
   else
     z

}

  def main(args: Array[String]): Unit = {
    var rst =max(2,3,5)
    println(s"2和3的较大者是:${rst}")

  }
}

函数返回多个值

例题:找到x y z 中的最大值和最小值

kotlin
 体验AI代码助手
 代码解读
复制代码
object basic35 {
  // 作用:找到x y z 中的最大值和最小值
  //(Int,Int)是返回值的类型 表示这个函数的返回值有两个整数
  def max(x:Int , y :Int,z:Int):(Int,Int)={
    var maxValue=if (x>y) x else y
    maxValue=if (maxValue>z) maxValue else z
    var minValue=if (x<y) x else y
    minValue=if (minValue<z)minValue else z

    (minValue,maxValue)
  }

  def main(args: Array[String]): Unit = {
    var rst =max(100,29,200)
    println(s"最小值:${rst._1},较大者${rst._2}")
  }
}

参数默认值:如果在调用的时候 没有传入

格式:参数名:类型 = 默认值

例题:

scss
 体验AI代码助手
 代码解读
复制代码
object basic37 {
 def greet( name:String="小张",age:Int=17,gender:String="女"):Unit={
   println(s"你好,我是${name},我今年${age}岁,我的性别是:${gender}")
 }

 def main(args: Array[String]): Unit = {
   greet("小花",18,"女")
   greet("小花",18)
   greet("小花")
   greet()


   //设置年龄为20 其他两个参数为默认值
   greet(age = 20)
   greet(gender = "男",age=20)
 }
}
参数类型:

(1) 可变参数:用来接受同一类型的多个参数

格式:参数类型*

代码案例:

scss
 体验AI代码助手
 代码解读
复制代码
scss
 体验AI代码助手
 代码解读
复制代码
object basic38 {


// 可变参数:用来接受同一类型的多个参数
// 格式: 参数类型*
def getSum(args:Int* ):Int={
  var sum=0
  for (i <- args){
    sum += i
  }
  sum
}

def main(args: Array[String]): Unit = {
  val rst1=getSum(1,2,3)
  val rst2=getSum(1,2,3,4)
  val rst3=getSum(1,2)
  println(rst1,rst2,rst3)
}
}