8.2.1 Kotlin中的函数

首先,我们来看下Kotlin中函数的概念。#### 函数声明

Kotlin 中的函数使用 fun 关键字声明

1. fun double(x: Int): Int {
2. return 2*x
3. }
4. ```#### 函数用法

6. 调用函数使用传统的方法

8. ```kotlin
9. fun test() {
10. val doubleTwo = double(2)
11. println("double(2) = $doubleTwo")
12. }

输出:double(2) = 4

调用成员函数使用点表示法

1. object FPBasics {

3. fun double(x: Int): Int {
4. return 2 * x
5. }

7. fun test() {
8. val doubleTwo = double(2)
9. println("double(2) = $doubleTwo")
10. }
11. }

13. fun main(args: Array<String>) {
14. FPBasics.test()
15. }

我们这里直接用object对象FPBasics来演示。