Local functions in Kotlin

Kotlin supports local functions, i.e. it allows you to put a function inside a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fun accumulate(number: Int): Int {
    var givenNumber = number
    fun add() {
        givenNumber++
    }
    for (i in 1..10) {
        add()
    }
    return givenNumber
}

fun calculateBMI(weightInKg: Double, heightInCm: Double): Double {
    fun calculateBMI(weightInKg: Double, heightInCm: Double): Double {
        val heightInMeter = heightInCm / 100
        return weightInKg / (heightInMeter * heightInMeter)
    }

    // Calculate BMI using the nested function
    return calculateBMI(weightInKg, heightInCm)
}

fun main(args: Array<String>) {
    println(accumulate(32))
    // 42

    println(calculateBMI(100.0, 200.0))
    // 25.0
}

Local functions can also access local variables of outer functions and help with code reuse.

Updated: