Skip to main content

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 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.
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.
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.
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.
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.
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.
@JvmOverloads
fun initialize(name: String, limit: Int = 100, strict: Boolean = true) {}
Generated Java signatures:
void initialize(String name, int limit, boolean strict);
void initialize(String name, int limit);
void initialize(String name);
Master Kotlin with Deep Grasping Methodology!Learn More