Kotlin Functions

Functions are reusable blocks of code declared with the fun keyword, and Kotlin adds default and named arguments to make calling them more flexible.

Declaring a Function

A Kotlin function is declared with the fun keyword, followed by a name, a parameter list in parentheses, and an optional return type after a colon. If a function doesn't return a meaningful value, its return type is Unit, which can be omitted.

A basic function

fun greet(name: String): String {
    return "Hello, $name!"
}

fun main() {
    val message = greet("Alice")
    println(message)
}

Single-Expression Functions

When a function body is a single expression, Kotlin lets you skip the curly braces and the return keyword by using an equals sign instead. The return type can usually be inferred, though it is good style to state it explicitly for public functions.

Single-expression function

fun square(x: Int): Int = x * x

fun main() {
    println(square(7))
}

Default Arguments

Parameters can declare a default value, making them optional at the call site. This removes the need for overloaded function signatures that Java-style code often requires - a single function definition can cover multiple calling patterns.

Default arguments

fun powerOf(base: Int, exponent: Int = 2): Int {
    var result = 1
    repeat(exponent) { result *= base }
    return result
}

fun main() {
    println(powerOf(5))       // exponent defaults to 2 -> 25
    println(powerOf(2, 10))   // explicit exponent -> 1024
}

Named Arguments

Named arguments let you specify which parameter a value belongs to, regardless of order. This is especially useful for functions with several parameters, or when several of them share a default value and you only want to override one that isn't first.

Named arguments

fun createUser(name: String, age: Int = 18, isAdmin: Boolean = false) {
    println("$name, age $age, admin=$isAdmin")
}

fun main() {
    createUser(name = "Sam", isAdmin = true)
    createUser(age = 30, name = "Priya")
}

Combining Defaults and Names

Default and named arguments are frequently used together: default values reduce the number of arguments a caller must supply, while named arguments make it clear and safe to override just the ones that matter, even out of order.

  • Parameters with defaults can be skipped entirely at the call site
  • Named arguments can appear in any order after positional ones
  • Once you use a named argument, remaining arguments are usually also named for clarity
  • vararg parameters accept a variable number of values and are typically declared last
FeatureSyntaxBenefit
Default argumentfun f(x: Int = 0)Fewer overloads needed
Named argumentf(x = 5)Clarity and reordering
Single-expressionfun f() = xLess boilerplate
Varargfun f(vararg n: Int)Accepts any number of values
Note: A function's return type is inferred automatically only for single-expression functions (fun f() = ...). Block-body functions with curly braces must declare their return type explicitly, or Kotlin assumes Unit.
Note: Positional arguments cannot follow named arguments. If you write f(name = "Al", 30), the compiler rejects it - once you switch to named form, keep using it for the rest of the call.

Exercise: Kotlin Functions

How do you give a function parameter a default value in Kotlin?