Single-Expression Functions / Local Functions
In Kotlin, when a function body consists of a single expression, you can omit the curly braces and return keyword by writing it as a "single-expression function." You can also define a "local function" inside another function to encapsulate logic within a specific scope.
Syntax
// Regular function
fun double(x: Int): Int {
return x * 2
}
// Single-expression function (more concise using =)
fun double(x: Int): Int = x * 2
// Return type can also be omitted via type inference.
fun double(x: Int) = x * 2
// Local function (defines a function inside another function)
fun process(numbers: List<Int>): List<Int> {
fun validate(n: Int): Boolean = n > 0 // Local function
return numbers.filter { validate(it) }
}
Syntax Overview
| Syntax | Description |
|---|---|
| fun name(p) = expr | Single-expression function. The expression on the right side of = is the return value. |
| Omitting the return type | You can omit the return type when it can be inferred from the expression. |
| Local function | A function defined inside another function. It can access variables from the outer scope. |
| Nested function | A local function can itself contain another local function. |
Sample Code
// Single-expression function examples
fun square(n: Int) = n * n
fun greet(name: String) = "Hello, $name!"
fun isEven(n: Int) = n % 2 == 0
fun max(a: Int, b: Int) = if (a > b) a else b
// Local function example
fun findDuplicates(numbers: List<Int>): List<Int> {
// Local function — can access the outer variable numbers.
fun hasDuplicate(n: Int): Boolean =
numbers.count { it == n } > 1
return numbers.filter { hasDuplicate(it) }.distinct()
}
// Local function with recursion
fun factorial(n: Int): Long {
fun calc(x: Int, acc: Long): Long = // Local function for tail recursion
if (x <= 1) acc else calc(x - 1, acc * x)
return calc(n, 1L)
}
fun main() {
// Calling single-expression functions
println(square(5)) // 25
println(greet("Kotlin")) // Hello, Kotlin!
println(isEven(4)) // true
println(max(10, 7)) // 10
// Calling the local function
val nums = listOf(1, 2, 3, 2, 4, 3, 5)
println(findDuplicates(nums)) // [2, 3]
// Factorial (tail recursion via local function)
println(factorial(5)) // 120
println(factorial(10)) // 3628800
// Combining single-expression functions
fun cube(n: Int) = n * square(n) // square is defined above
println(cube(3)) // 27
}
Details
Single-expression functions use = to define a function on a single line, making simple transformation or utility functions concise. However, when the logic becomes more complex, the regular curly-brace style is usually easier to read.
A local function is a closure that can access variables from its enclosing function. Grouping auxiliary logic that should not be called from outside into a local function improves code clarity. Lambda expressions can serve a similar purpose, but local functions have the advantage of supporting recursion more naturally.
For basic function definitions, see fun — Function Definition. For lambdas, see Lambda Expressions.
If you find any errors or copyright issues, please contact us.