WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kotlin · Basic · question 9 of 100

How does Kotlin handle default and named arguments in function calls? Provide an example.?

📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

Kotlin allows you to specify default argument values for functions, which means that you can call functions without passing all of their arguments explicitly. With named arguments, you can also specify the arguments in any order you like by using their names.

Here is an example of a function that takes two arguments but has default values for both:

fun greet(name: String = "World", greeting: String = "Hello") {
    println("$greeting, $name!")
}

This function takes two arguments, ‘name‘ and ‘greeting‘, but provides default values for both of them. If you call the function without any arguments, it will use the default values:

greet() // Prints "Hello, World!"

If you want to provide some arguments but not others, you can use named arguments:

greet(greeting = "Hi") // Prints "Hi, World!"
greet(name = "Alice") // Prints "Hello, Alice!"

Notice that you can specify the arguments in any order you like, as long as you use their names.

In addition, default and named arguments can be combined with variable arguments to create even more flexible functions.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kotlin interview — then scores it.
📞 Practice Kotlin — free 15 min
📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

All 100 Kotlin questions · All topics