- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 193字
- 2021-07-23 16:25:13
Declaring and calling functions
A Scala function takes 0 to n parameters and returns a value. The type of each parameter must be declared. The type of the returned value is optional, as it is inferred by the Scala compiler when not specified. However, it is a good practice to always specify the return type, as it makes the code more readable:
scala> def presentation(name: String, age: Int): String =
"Hello, my name is " + name + ". I am " + age + " years old."
presentation: (name: String, age: Int)String
scala> presentation(name = "Bob", age = 25)
res1: String = Hello, my name is Bob. I am 25 years old.
scala> presentation(age = 25, name = "Bob")
res2: String = Hello, my name is Bob. I am 25 years old.
We can call a function by passing arguments in the right order, but we can also name the arguments and pass them in any order. It is a good practice to name the arguments when some of them have the same type, or when a function takes many arguments. It avoids passing the wrong argument and improves readability.
推薦閱讀
- 自動(dòng)駕駛網(wǎng)絡(luò):自智時(shí)代的網(wǎng)絡(luò)架構(gòu)
- 電子政務(wù)效益的經(jīng)濟(jì)分析與評(píng)價(jià)
- Hands-On Chatbots and Conversational UI Development
- HCNA網(wǎng)絡(luò)技術(shù)
- 計(jì)算機(jī)網(wǎng)絡(luò)與數(shù)據(jù)通信
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- 網(wǎng)絡(luò)安全技術(shù)與解決方案(修訂版)
- PLC、現(xiàn)場(chǎng)總線及工業(yè)網(wǎng)絡(luò)實(shí)用技術(shù)速成
- 區(qū)塊鏈輕松上手:原理、源碼、搭建與應(yīng)用
- Yii Application Development Cookbook(Second Edition)
- 圖解手機(jī)元器件維修技巧
- Kong網(wǎng)關(guān):入門、實(shí)戰(zhàn)與進(jìn)階
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 物聯(lián)網(wǎng)M2M開(kāi)發(fā)技術(shù):基于無(wú)線CPU-Q26XX
- 企業(yè)網(wǎng)絡(luò)組建與維護(hù)項(xiàng)目式教程