Kotlin supports default arguments and named parameters, which provides greater flexibility compared to Java.
fun greet(name: String = "User", greeting: String = "Hello") { println("$greeting, $name!") }
greet(greeting = "Hi") // -> Hi, User!
@JvmOverloads annotation may be required.Can you mix positional and named arguments in any order when calling a function in Kotlin?
Correct answer: No, after you specify at least one named argument, all subsequent arguments must also be named. Violating this will cause a compilation error.
// Incorrect greet(greeting = "Hi", "Ivan") // Error! // Correct greet("Ivan", greeting = "Hi") greet(name = "Ivan", greeting = "Hi")
Story
The team integrated a Kotlin module with a legacy Java project and forgot to add the
@JvmOverloadsannotation for the function with default parameters. As a result, the Java code did not see the necessary overload methods — runtime errors occurred during calls.
Story
During refactoring using named parameters, a developer accidentally switched the arguments — subsequent renaming of parameters went unnoticed (the typing was not violated, but the call semantics changed!). This led to strange bugs in the UI logic, discovered later.
Story
One of the developers, in an attempt to improve readability, mixed positional and named arguments in the middle of the call. The code did not compile, but the team struggled to understand what the problem was — as they often encountered this in other languages and expected similar behavior from Kotlin.