Kotlin Named and Default Arguments
Make optional receipt details clear at the call site.
How it works
Default parameter values let callers omit routine arguments. Named arguments identify a value by parameter name and make adjacent same-type values less ambiguous. Changing a public parameter name can affect callers using named arguments, so treat names as part of the Kotlin API contract.
Run this Kotlin example
These are local Kotlin/JVM programs. Install a JDK and the compiler using the official Kotlin compiler guide, or use an IDE with Kotlin support. Check java -version and kotlinc -version in a new terminal. The reviewed compiler is Kotlin 2.4.21 with a JVM 21 target, executed on Temurin JDK 25.0.5. Use JDK 21 or newer for this target. Android, Kotlin/JS and Kotlin/Native require separate platform projects and are not the tested environment here.
Save the complete source as arguments-defaults.kt in its own learning folder. The package line gives this example a namespace, not a network address. Bhylu has no Kotlin browser runner; do not use the JavaScript or Python editor. Apache, PHP and MySQL are not required.
Compile with kotlinc arguments-defaults.kt -jvm-target 21 -include-runtime -d lesson.jar, then run java -jar lesson.jar. Use a learning folder you own and keep generated JARs outside the website. These sources require no third-party application libraries.
Download the complete arguments-defaults.kt · Download all source files with hash checks
For the optional collection, save download-examples.mjs in a new learning folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/kotlin/. It refuses an existing bhylu-kotlin-examples folder and installs or executes no Kotlin code. Compile one ordinary lesson at a time by filename.
Example
package bhylu.lessons.k_arguments_defaults
fun receipt(bike: String, minutes: Int = 30): String {
require(minutes > 0)
return "$bike: $minutes minutes"
}
fun main() {
println(receipt("B-31"))
println(receipt(bike = "B-18", minutes = 45))
}
Expected result
B-31: 30 minutes B-18: 45 minutes
The first call uses the declared default; the second states both names. The helper validates the positive duration instead of trusting a convenient default to make all calls valid.
For exact language and library contracts, consult the official Kotlin documentation. These fictional fixtures are learning programs, not a deployed hire service.