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.

  1. Home
  2. Kotlin Dictionary
  3. Defining Functions with fun

Defining Functions with fun

Functions in Kotlin are defined with the fun keyword. Parameters can have default values, and using named arguments at the call site improves readability.

Syntax

// Basic function definition
fun functionName(paramName: Type, paramName: Type): ReturnType {
    // body
    return value
}

// Function with no return value (Unit can be omitted)
fun greet(name: String): Unit {
    println("Hello, $name!")
}

// Default parameter
fun power(base: Int, exponent: Int = 2): Int {
    return Math.pow(base.toDouble(), exponent.toDouble()).toInt()
}

// Call with named arguments
power(base = 3, exponent = 4)
power(exponent = 3, base = 2)  // Order can be changed

Syntax Reference

SyntaxDescription
fun name(params): TypeDefines a function. The return type can be omitted when it is Unit.
paramName: Type = defaultValueSets a default value for a parameter. The argument can be omitted at the call site.
Named argumentsSpecifying paramName = value at the call site lets you pass arguments in any order.
vararg paramName: TypeDefines a variable-length parameter. The caller can pass any number of values.
return valueReturns a value from the function. Can be omitted when there is no return value.

Sample Code

// Basic function definition
fun add(a: Int, b: Int): Int {
    return a + b
}

// No return value (Unit can be omitted)
fun printMessage(msg: String) {
    println(msg)
}

// Default parameter
fun greet(name: String, greeting: String = "Hello") {
    println("$greeting, $name!")
}

// Variable-length parameter (vararg)
fun sum(vararg numbers: Int): Int {
    return numbers.sum()
}

// Using named arguments
fun createUser(name: String, age: Int = 0, admin: Boolean = false): String {
    return "User(name=$name, age=$age, admin=$admin)"
}

fun main() {
    println(add(3, 5))          // 8
    printMessage("Kotlin!")     // Kotlin!

    greet("Alice")              // Hello, Alice!
    greet("Bob", "Hi")          // Hi, Bob!
    greet(greeting = "Hey", name = "Charlie")  // Named arguments change the order

    println(sum(1, 2, 3, 4, 5)) // 15

    // Combining default parameters and named arguments
    println(createUser("Alice"))                        // age and admin use defaults
    println(createUser("Bob", age = 25))                // Only age is specified
    println(createUser("Carol", age = 30, admin = true))

    // Spread an array into a vararg parameter (spread operator)
    val nums = intArrayOf(10, 20, 30)
    println(sum(*nums))         // 60
}

Notes

By combining default parameters and named arguments, Kotlin lets you design flexible APIs without the method overloading required in Java. For functions with many parameters, named arguments make the call site much easier to read.

When the return type is unambiguous, type inference allows you to omit it, but explicitly declaring it is recommended for public APIs.

For concise functions, see Single-expression functions / Local functions. To learn how to treat functions as values, see Higher-order functions.

If you find any errors or copyright issues, please .