> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Default Parameter

Kotlin allows function parameters to specify default values, which are automatically applied when the corresponding arguments are omitted during a function invocation. This mechanism is resolved at compile-time and reduces boilerplate by eliminating the need for explicit method overloading.

## Syntax

Default values are defined by appending the assignment operator (`=`) and the value expression to the parameter declaration.

```kotlin theme={"dark"}
fun configure(timeout: Int, retries: Int = 3, protocol: String = "HTTPS") {}
```

## Evaluation Mechanics

Default parameter expressions are evaluated at **call time** (on each invocation), but the evaluation occurs within a compiler-generated synthetic method at the **declaration site**. This distinction means that default expressions are evaluated in the lexical scope of the declaration, allowing them to access `private` members of the enclosing class or file, which would be inaccessible if they were evaluated at the call site.

```kotlin theme={"dark"}
class IdGenerator {
    private fun generateUuid(): String = java.util.UUID.randomUUID().toString()
    
    // Default expression accesses a private method from the declaration site
    fun process(id: String = generateUuid()) {}
}
```

## Argument Resolution Rules

**Trailing Default Parameters**
If default parameters are positioned at the end of the parameter list, callers can omit them using standard positional arguments.

```kotlin theme={"dark"}
fun connect(host: String, port: Int = 8080) {}

fun main() {
    connect("localhost") // Valid: port defaults to 8080
}
```

**Preceding Default Parameters**
If a parameter with a default value precedes a parameter without a default value, the default value can only be utilized if the caller uses **named arguments** for the subsequent arguments they *actually provide* in the call. Any subsequent parameters that also have default values can simply be omitted.

*Exception:* If the final parameter is a functional type (lambda), it can be passed outside the parentheses as a trailing lambda. In this scenario, the lambda itself does not need to be named, and the default value of a preceding parameter can be utilized, provided that all other required preceding parameters are explicitly supplied positionally or by name.

```kotlin theme={"dark"}
fun test(a: Int = 1, b: String, c: Int = 2) {}
fun execute(host: String, retries: Int = 3, action: () -> Unit) {}

fun main() {
    // Valid: 'a' and 'c' use defaults, 'b' is explicitly provided via named argument
    test(b = "x") 

    // Valid: 'host' is provided positionally, 'retries' uses default, 'action' is a trailing lambda
    execute("localhost") { println("Running") } 
}
```

## Inheritance and Overriding

When overriding a method that declares default parameters, the overriding method inherits the default values from the base type. The overriding signature **must omit** the default value declarations; redefining them is prohibited by the compiler to prevent ambiguity.

```kotlin theme={"dark"}
open class Base {
    open fun execute(retries: Int = 5) {}
}

class Derived : Base() {
    // Cannot specify `retries: Int = 5` or any other default here
    override fun execute(retries: Int) {} 
}
```

## Java Interoperability (`@JvmOverloads`)

Because the Java language does not natively support default parameters, a Kotlin function with default parameters is exposed to Java as a single method signature requiring all arguments.

To expose overloaded methods to Java callers, the `@JvmOverloads` annotation must be applied. This instructs the Kotlin compiler to generate a sequence of overloaded methods, dropping one default parameter at a time from right to left.

```kotlin theme={"dark"}
@JvmOverloads
fun initialize(name: String, limit: Int = 100, strict: Boolean = true) {}
```

*Generated Java signatures:*

```java theme={"dark"}
void initialize(String name, int limit, boolean strict);
void initialize(String name, int limit);
void initialize(String name);
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
