函数的基本使用

48 阅读1分钟
[toc]
# 函数的基本使用

函数的基本使用

1. 函数的定义;

2. 函数的调用;

3. 函数的返回值。

知识目标:

1. 了解函数的定义,使用方法;

2. 掌握函数的返回值的用法。

能力目标:

1. 能正确定义和调用函数;

2. 能正确设置函数的返回值。

(一)定义函数

image.png


![image.png](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/219566fcf81645889ec2e3c079d413bc~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5rSL6IqL5aSn6a2U546L:q75.awebp?rk3s=f64ab15b&x-expires=1772422382&x-signature=pv0iBMKXpq8PvBxReE9%2BaPO4mMs%3D)
object l18 {

  def max(x: Int, y: Int):Int={
    if(x>y) x else y
      }
  def say() :Unit={
    println("hello worls!")
  }

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

  }

}


object l20 {
  def max(x: Int,y: Int,z:Int):Int={
    val rst= if(x>y) x else y
    if(rst>2) rst else z
  }
  def main(args:Array[String]):Unit={

    var rst=max(100,20,200)
    println(s"较大者是:${rst}")
  }

}