Back to questions
JuniorTheoryCommon
What does the program print?
fun greet(
name: String,
greeting: String = "Hello",
mark: String = "!"
) = "$greeting, $name$mark"
fun main() {
println(greet("Ann", mark = "?"))
}Hello, Ann?
- Expecting
?, Ann!: thinking the second argument always goes to the second parameter. A named argument goes to the parameter with its name, heremark. - Expecting a compile error because
greetingis skipped: a parameter with a default may be left out, and a named argument lets the call jump over it.
- What does
greet("Ann", "Hi")print? - What does
greet(mark = "?", name = "Ann")print, and why is that order allowed?