Functions

Functions can be single expressions with no braces or return, and default/named arguments let one function replace what Java needs several overloads to cover.

Declaring a function

Functions start with fun, and the return type comes after the parameter list, separated by a colon:

</> main.kt
fun add(a: Int, b: Int): Int {
    return a + b
}

fun main() {
    println(add(3, 4))
}
Output
7

Single-expression functions

When a function's body is a single expression, you can skip the braces and return entirely, using = instead:

</> main.kt
fun square(n: Int): Int = n * n

fun main() {
    println(square(6))
}
Output
36

The return type here can even be omitted and inferred from the expression — fun square(n: Int) = n * n compiles identically, since the compiler can see n * n is an Int.

Default and named arguments

Parameters can have default values, and you can call a function naming which argument is which — both features Java lacks entirely:

</> main.kt
fun greet(name: String, greeting: String = "Hello"): String {
    return "$greeting, $name!"
}

fun main() {
    println(greet("Priya"))
    println(greet("Sam", "Welcome"))
    println(greet(greeting = "Hey", name = "Alex"))
}
Output
Hello, Priya!
Welcome, Sam!
Hey, Alex!

Named arguments can be passed in any order once you name them, which is especially useful for functions with several optional parameters — no need for a pile of overloads just to cover different combinations of defaults.

Note: Default parameter values are one of the reasons Kotlin code needs far fewer overloaded function signatures than the equivalent Java — a single function with two or three optional parameters replaces what Java typically needs three or four separate overloads to express.