← Back to Kotlin Course | Chapter 4: Functions | Lesson 2 of 7

Default Parameters

Default parameters let a function already know what value to use if you don't bother giving it one yourself.
Syntax
kotlin
fun functionName(param: Type, optional: Type = defaultValue): ReturnType {
    // body
}

functionName(value)
functionName(value, otherValue)

Declaring a Default Value

Assigning a value directly in the parameter list, like greeting: String = "Hello", makes that parameter optional -- callers who don't pass it get the default.

Example: Declaring a Default Value

markup
// Define the function `greet` taking `name`, `greeting` and returning a String
fun greet(name: String, greeting: String = "Hello"): String {
    // Return `"$greeting, $name!"` from this function
    return "$greeting, $name!"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `greet("Kotlin")` to the console, with a trailing newline
    println(greet("Kotlin"))
}

Overriding a Default

A caller can still pass a value for a parameter that has a default, which simply overrides that default for this particular call.

Example: Overriding a Default

markup
// Define the function `greet` taking `name`, `greeting` and returning a String
fun greet(name: String, greeting: String = "Hello"): String {
    // Return `"$greeting, $name!"` from this function
    return "$greeting, $name!"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `greet("Kotlin", "Welcome")` to the console, with a trailing newline
    println(greet("Kotlin", "Welcome"))
}

Multiple Default Parameters

A function can have several parameters with defaults, letting callers omit any combination of the trailing ones freely.

Example: Multiple Default Parameters

markup
// Define the function `createUser` taking `name`, `age`, `active` and returning a String
fun createUser(name: String, age: Int = 18, active: Boolean = true): String {
    // Return `"$name (age $age, active=$active)"` from this function
    return "$name (age $age, active=$active)"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `createUser("Alice")` to the console, with a trailing newline
    println(createUser("Alice"))
    // Print `createUser("Bob", 25)` to the console, with a trailing newline
    println(createUser("Bob", 25))
}

Defaults Avoid Overloading

Instead of writing several overloaded functions for each combination of arguments, a single function with defaults covers all the common cases concisely.

Example: Defaults Avoid Overloading

markup
// Define the function `power` taking `base`, `exponent` and returning an Int
fun power(base: Int, exponent: Int = 2): Int {
    // Declare a mutable variable `result`, initialized to `1`
    var result = 1
    repeat(exponent) { result *= base }
    // Return `result` from this function
    return result
}

// Entry point: execution of the program starts here
fun main() {
    // Print "Square: ${power(5)}" to the console, with a trailing newline
    println("Square: ${power(5)}")
    // Print "Cube: ${power(5, 3)}" to the console, with a trailing newline
    println("Cube: ${power(5, 3)}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming Kotlin needs overloaded functions like Java for optional parameters, instead of using a single function with defaults.
  2. Putting a non-default parameter after a parameter with a default value without using named arguments, which can force awkward call sites.
  3. Forgetting that default values are re-evaluated for every call, not computed once and shared across calls.
Chapter Summary
  • A default value is written as parameter: Type = value in the function declaration.
  • Callers may omit any parameter that has a default, and the default value is used instead.
  • Default parameters remove the need for multiple overloaded functions in many cases.
  • Combining default parameters with named arguments makes it easy to skip some defaults while overriding others.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.