Kotlin's support for default and named arguments makes it easy to provide sensible defaults for function parameters and to specify only the arguments that differ from the defaults.
Consider a function that formats a string with a date and time:
fun formatDateTime(
date: String,
time: String,
dateFormat: String = "yyyy-MM-dd",
timeFormat: String = "HH:mm:ss"
): String {
return "$date $time"
}
This function has four parameters, two of which have default values. When calling the function, we can provide values for the first two parameters, and the default values will be used for the latter two:
formatDateTime("2019-01-01", "12:00:00") // "2019-01-01 12:00:00"
We can also provide values for all four parameters:
formatDateTime(
date = "2019-01-01",
time = "12:00:00",
dateFormat = "dd/MM/yy",
timeFormat = "hh:mm a"
) // "01/01/19 12:00 PM"
When calling a function with multiple parameters, the order of the parameters is important. This can make the code difficult to read, especially when some of the parameters have default values.
Kotlin's support for named arguments lets us specify the parameter name along with the value, which makes the code easier to read:
fun formatDateTime(
date: String,
time: String,
dateFormat: String = "yyyy-MM-dd",
timeFormat: String = "HH:mm:ss"
): String {
return "$date $time"
}
formatDateTime(
time = "12:00:00",
date = "2019-01-01",
timeFormat = "hh:mm a",
dateFormat = "dd/MM/yy"
) // "01/01/19 12:00 PM"
In the example above, we've specified the parameter names along with the values. This makes the code easier to read, especially when some of the parameters have default values.
When calling a function with multiple parameters, we can specify only the arguments that differ from the defaults. For example, consider the following function:
fun formatDateTime(
date: String,
time: String,
dateFormat: String = "yyyy-MM-dd",
timeFormat: String = "HH:mm:ss"
): String {
return "$date $time"
}
This function has four parameters, two of which have default values. We can call this function with two arguments, and the default values will be used for the remaining parameters:
formatDateTime("2019-01-01", "12:00:00") // "2019-01-01 12:00:00"
We can also specify only the arguments that differ from the defaults:
formatDateTime(
date = "2019-01-01",
time = "12:00:00",
timeFormat = "hh:mm a"
) // "2019-01-01 12:00:00"
In the example above, we've specified values for the date
and time
parameters, and the default value will be used for the dateFormat
parameter.
Kotlin's support for default and named arguments makes it easy to provide sensible defaults for function parameters and to specify only the arguments that differ from the defaults. This can make the code easier to read and understand.