Single-Expression Functions
In this page:
fun functionName(param: Type) = expression
fun functionName(param: Type): ReturnType = if (condition) a else b
Writing a Single-Expression Function
Instead of { return ... }, a function body can be a single = expression, which is both shorter and often clearer for simple computations.
Example: Writing a Single-Expression Function
fun square(x: Int) = x * x
// Entry point: execution of the program starts here
fun main() {
// Print "Square of 6: ${square(6)}" to the console, with a trailing newline
println("Square of 6: ${square(6)}")
}
Login to try C/C++/Java/PHP code in the editor
Return Type Inference
Kotlin infers the return type of a single-expression function from the expression itself, so it is usually unnecessary to write it explicitly.
Example: Return Type Inference
fun greeting(name: String) = "Hello, $name!"
// Entry point: execution of the program starts here
fun main() {
// Print `greeting("Kotlin")` to the console, with a trailing newline
println(greeting("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
Using if/when Expressions
Because if and when are expressions in Kotlin, a single-expression function body can contain fairly rich logic while remaining just one expression.
Example: Using if/when Expressions
fun classify(n: Int) = if (n % 2 == 0) "even" else "odd"
// Entry point: execution of the program starts here
fun main() {
// Print "7 is ${classify(7)}" to the console, with a trailing newline
println("7 is ${classify(7)}")
// Print "8 is ${classify(8)}" to the console, with a trailing newline
println("8 is ${classify(8)}")
}
Login to try C/C++/Java/PHP code in the editor
When Not to Use This Style
Single-expression syntax is best kept for short, genuinely single-expression logic; anything requiring multiple statements or intermediate variables should use a normal block body instead.
Example: When Not to Use This Style
// Define the function `describeNumber` taking `n` and returning a String
fun describeNumber(n: Int): String {
// Declare a read-only variable `parity`, set to `if (n % 2 == 0) "even" else "odd"`
val parity = if (n % 2 == 0) "even" else "odd"
// Declare a read-only variable `sign`, set to `if (n >= 0) "non-negative" else "negative"`
val sign = if (n >= 0) "non-negative" else "negative"
// Return `"$n is $parity and $sign"` from this function
return "$n is $parity and $sign"
}
// Entry point: execution of the program starts here
fun main() {
// Print `describeNumber(-4)` to the console, with a trailing newline
println(describeNumber(-4))
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the
=sign; single-expression functions use= expressioninstead of a block body. - Trying to write multiple statements in a single-expression function; it must be exactly one expression.
- Adding an unnecessary explicit return type when it can be safely inferred from the expression.
- A single-expression function is written as
fun name(params) = expression, without curly braces. - The return type can usually be inferred from the expression, so it is often omitted.
- This style suits short, simple computations, not multi-statement logic.
- A single-expression function still supports parameters and default values just like a block-bodied one.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: