Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Kotlin Dictionary

  1. Home
  2. Kotlin Dictionary
  3. Single-Expression Functions / Local Functions

Single-Expression Functions / Local Functions

Since: Kotlin 1.0(2016)

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

SyntaxDescription
fun name(p) = exprSingle-expression function. The expression on the right side of = is the return value.
Omitting the return typeYou can omit the return type when it can be inferred from the expression.
Local functionA function defined inside another function. It can access variables from the outer scope.
Nested functionA local function can itself contain another local function.

Sample Code

sample_single_expr_local_function.kt
// 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
}
single_expr_local_function.kt
kotlinc single_expr_local_function.kt -include-runtime -d single_expr_local_function.jar
java -jar single_expr_local_function.jar
25
Hello, Kotlin!
true
10
[2, 3]
120
3628800
27

Common Mistakes

sample_single_expr_local_function_mistakes.kt
// trying to write multiple statements in a single-expression function
// A single-expression function can only contain one expression, not multiple statements
// fun setup(name: String) = { // this becomes a function that returns a lambda
//     println("Name: $name") // this is inside the lambda body
//     name.uppercase() // this is also not executed
// }
// Use the regular block-body style when multiple operations are needed

// calling a local function before it is defined
fun computeScore(values: List<Int>): Int {
    // validate(0) // compile error: local functions can only be used after they are defined
    fun validate(n: Int) = n >= 0 // definition goes here
    return values.filter { validate(it) }.sum()
}

// overlooking that a single-expression function can return Unit
// When the expression returns Unit (e.g., println), Unit is inferred as the return type
// If you intended a different return type, this silently becomes Unit
fun printGreeting(name: String) = println("Hello, $name!")
// Return type is Unit (because println returns Unit) — using the value gives kotlin.Unit

fun main() {
    println(computeScore(listOf(-1, 5, 3, -2, 8)))
    printGreeting("Makise Kurisu")
    val ret = printGreeting("Okabe Rintaro")
    println("Return value: $ret") // Return value: kotlin.Unit
}

The command looks like this:

kotlinc sample_single_expr_local_function_mistakes.kt -include-runtime -d sample_single_expr_local_function_mistakes.jar
java -jar sample_single_expr_local_function_mistakes.jar
16
Hello, Makise Kurisu!
Hello, Okabe Rintaro!
Return value: kotlin.Unit

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 .