💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 函数 ![](https://img.kancloud.cn/e9/09/e909ab0b726f24de93c5732640ba2d92_701x418.png) ~~~scala def main(args: Array[String]): Unit = { println(add(2,3)) println(three())//没有参数,括号可以省略。 sayHello() } def add(x:Int,y:Int):Int={ x+y //最后一行就是返回值,不用return } def three() = 1+2 //Unit意思是无返回值 def sayHello():Unit={ println("sayHello") } ~~~ * 默认参数 ~~~ def sayName(name:String = "Bizz"):Unit= { println(name); } ~~~ * 命名参数 ~~~ def speed(distance:Float,time:Float):Float={ distance/time } println(speed(10,2)) println(speed(time=2,distance = 10)) ~~~ * 可变参数 加上*表示可以传多个值 ~~~ def add(number:Int*)={ var result = 0; for (num<-number){ result+=num; } result } println(add(1,2,3,4,5,6,7)) ~~~ * 循环表达式 ![](https://img.kancloud.cn/ae/85/ae85654c38ccf89947c94a11b2b77181_886x300.png) ~~~ for(i <- 1 to 10){ println(i) } val names = Array("bizz","james","wade"); //names里面的所有元素作用一个方法 names.foreach(name=>println(name)); var (num,sum) = (100,0); while (num>0){ sum = sum+num num = num-1 } println(sum) } ~~~