Kotlin Function Default Arguments
Kotlin functions can assign default values to parameters. When a caller omits one of these arguments, Kotlin uses the corresponding default value from the function declaration. This reduces the need to create several overloaded functions for common combinations of values.
A function may contain both required parameters and parameters with default values. Placing optional parameters after required parameters usually makes positional calls easier to read. Named arguments can be used when a specific optional parameter needs to be supplied while another one is omitted.
Syntax for Kotlin default arguments
Assign a value to a parameter in the function declaration to make that parameter optional at the call site.
fun funName(param1 : Type, param2: Type = DefaultValue){ }
- An argument is optional when its parameter has a default value.
- An argument remains required when its parameter does not have a default value.
- The parameter type can be declared explicitly or inferred from the default value where Kotlin syntax permits inference.
- The default expression is evaluated when the function is called and that argument is omitted.
Example 1: Call a Kotlin function with default arguments
The following add() function gives both parameters a default value of 1. Calling it with two arguments uses the supplied values, while calling it without arguments uses both defaults.
Kotlin Program – example.kt
fun main(args: Array<String>) {
// function call with arguments
val sum1 = add(5, 6) // output : 11
println(sum1)
// function call with no arguments
val sum2 = add() // output : 2
println(sum2)
}
/**
* add function with parameters a, b
* a : default argument is 1
* b : default argument is 1
*/
fun add(a:Int = 1, b:Int = 1): Int {
return a + b
}
Output
11
2
The first call evaluates 5 + 6. In the second call, both arguments are omitted, so the function evaluates 1 + 1.
Example 2: Mix required and default parameters in Kotlin
The next function requires a and b, while c and d default to zero. It can therefore add two, three, or four supplied integers.
Kotlin Program – example.kt
fun main(args: Array<String>) {
val sum1 = add(5, 6)
val sum2 = add(5, 6, 7)
val sum3 = add(5, 6, 7, 8)
println(sum1)
println(sum2)
println(sum3)
}
/**
* add function with two, three or four integer arguments
* c : default argument is 0
* d : default argument is 0
*/
fun add(a:Int, b:Int, c:Int = 0, d:Int = 0): Int {
return a + b + c + d
}
Output
11
18
26
In the first call, both optional parameters use zero. The second call supplies c but omits d. The third call supplies all four values.
Skip a default parameter with Kotlin named arguments
Named arguments let a call identify parameters by name. They are useful when a function has several optional parameters or when the caller needs to omit an earlier optional argument but provide a later one.
fun formatUser(
name: String,
prefix: String = "User",
uppercase: Boolean = false
): String {
val text = "$prefix: $name"
return if (uppercase) text.uppercase() else text
}
fun main() {
println(formatUser("Maya"))
println(formatUser("Maya", prefix = "Admin"))
println(formatUser("Maya", uppercase = true))
}
Output
User: Maya
Admin: Maya
USER: MAYA
The third call leaves prefix at its default value while explicitly setting uppercase. Without a named argument, a positional value in that location would apply to prefix.
Kotlin positional and named argument rules
Keep these call-site rules in mind when using defaults:
- Positional arguments are matched to parameters from left to right.
- A named argument can select a later parameter while earlier optional parameters use their defaults.
- After skipping a parameter with a default value, use named arguments for later values to make the intended parameter clear.
- Named arguments depend on parameter names, so they may be unsuitable when calling some Java APIs because Java bytecode does not always expose stable parameter names.
Use an earlier parameter in a Kotlin default value
A default expression can refer to a parameter declared before it. This is useful when an optional value should be derived from a required value.
fun createLabel(
name: String,
label: String = "Account: $name"
): String {
return label
}
fun main() {
println(createLabel("Asha"))
println(createLabel("Asha", "Primary account"))
}
Output
Account: Asha
Primary account
The expression for label is evaluated only when the caller omits that argument. A later parameter cannot be referenced from an earlier parameter’s default expression.
Kotlin default arguments with function overrides
When overriding a function, do not repeat default parameter values in the overriding declaration. The default values belong to the base declaration, while the implementation executed at runtime can come from the derived class.
open class MessagePrinter {
open fun printMessage(message: String = "No message") {
println(message)
}
}
class ConsolePrinter : MessagePrinter() {
override fun printMessage(message: String) {
println("Console: $message")
}
}
fun main() {
val printer: MessagePrinter = ConsolePrinter()
printer.printMessage()
}
Output
Console: No message
Call Kotlin default arguments from Java
Java source code does not use Kotlin’s default-argument call syntax directly. For functions that must be convenient to call from Java, @JvmOverloads can generate overloads by removing trailing parameters that have default values.
class Greeter {
@JvmOverloads
fun greet(
name: String,
greeting: String = "Hello",
punctuation: String = "!"
): String {
return "$greeting, $name$punctuation"
}
}
This declaration provides Java-friendly overloads corresponding to greet(name), greet(name, greeting), and greet(name, greeting, punctuation). Because overloads are generated from trailing default parameters, arrange parameters with Java interoperability in mind.
Kotlin default arguments compared with function overloading
Default arguments are suitable when several calls perform the same operation and differ only in omitted values. Function overloading remains useful when variants accept meaningfully different types, implement different behavior, or need distinct Java-visible signatures. Avoid adding overloads that merely duplicate combinations already expressed clearly by default and named arguments.
Kotlin default argument FAQ
What is a default argument in Kotlin?
A default argument is a value assigned to a function parameter in its declaration. Kotlin uses that value when the caller omits the corresponding argument.
How do I skip one optional argument in a Kotlin function call?
Use the name of the later parameter. For example, formatUser("Maya", uppercase = true) skips the default prefix argument and supplies only uppercase.
Must Kotlin default parameters appear last?
No. Kotlin permits a parameter with a default value before a required parameter. However, calls may then need named arguments to omit the earlier parameter, so placing required parameters first is often clearer.
Can a Kotlin default argument call another function?
Yes. A default value can be an expression, including a function call. The expression runs each time the function is invoked without that argument, so avoid unexpected or unnecessarily expensive work in the default expression.
Can Java callers omit Kotlin default arguments?
Not through ordinary Java call syntax. Add explicit overloads or use @JvmOverloads when appropriate to generate Java-callable overloads for trailing default parameters.
Kotlin default arguments editorial QA checklist
- Verify that every omitted argument has a default value in the function declaration.
- Confirm that positional arguments map to the intended parameters from left to right.
- Use named arguments when skipping an optional parameter or clarifying similar values.
- Check that default expressions do not perform unexpected work or rely on unavailable later parameters.
- For Java-facing APIs, verify whether explicit overloads or
@JvmOverloadsare required.
Summary of Kotlin function default arguments
Kotlin default arguments make selected function parameters optional and reduce repetitive overloads. Combine them with named arguments when a call must skip a value or identify parameters clearly. For Java interoperability, consider generated or explicit overloads. Continue with the Kotlin Tutorial for related function examples.
TutorialKart.com