Default & Named Arguments
Kotlin is a modern, concise language that lets functions declare default parameter values and lets callers pass arguments by name — together they replace the tangle of overloaded methods found in other languages.
Learn Default & Named Arguments in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll write flexible functions with defaults and call them with clear, self-documenting named arguments.
What You'll Learn in This Lesson
1️⃣ Default Argument Values
A parameter can declare a default with = . If the caller omits that argument, the default is used. This means one function definition can serve many call patterns — no overloading required.
A single greet function handled three different call styles. In Java this would have meant three separate overloaded methods.
2️⃣ Named Arguments
When calling a function you can label each argument with its parameter name. This makes calls self-documenting, lets you reorder arguments, and — crucially — lets you skip earlier defaulted parameters to set just a later one.
makeBox(height = 10) sets only the height and leaves width and depth at their defaults — impossible with purely positional calls.
Your turn. Replace the TODO , then run and compare.
Mini-Challenge: A Flexible Logger
Write one log function with two defaulted parameters, then call it three different ways using named arguments.
Common Errors (and the fix)
- "Mixing named and positional arguments is not allowed" — Once you use a named argument, all following arguments must also be named. Put positional ones first.
- "No value passed for parameter" — A parameter without a default was omitted. Either pass it or give it a default value.
- Wrong parameter name — A typo in a named argument ( colour = ... vs color = ... ) won't compile. Match the declared name exactly.
- Expecting Java to see defaults — Java callers don't see Kotlin defaults unless you add @JvmOverloads . Within Kotlin it just works.
Pro Tips
- 💡 Name boolean arguments: connect(secure = true) reads far better than connect(true) .
- 💡 Defaults can use earlier params: a later default may reference an earlier parameter's value.
- 💡 Order defaults thoughtfully: put the most-often-overridden parameters earlier so positional calls stay natural.
📋 Quick Reference — Arguments
Concept
Kotlin Syntax
Default value
fun f(x: Int = 1)
Call with default
f()
Named argument
f(x = 5)
Skip earlier default
makeBox(depth = 2)
Java interop
@JvmOverloads
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ Default values let callers omit arguments
- ✅ One function with defaults replaces many overloads
- ✅ Named arguments make calls self-documenting
- ✅ Named arguments let you skip earlier defaults
- ✅ Positional arguments must come before named ones
- ✅ Next lesson: Collections (List, Set, Map)
Practice quiz
How do you give a Kotlin function parameter a default value?
- With the default keyword
- With a ? after the name
- With = after the type, e.g. greeting: String = "Hello"
- Defaults are not supported
Answer: With = after the type, e.g. greeting: String = "Hello". A parameter declares a default with =, used when the caller omits it.
What is a key benefit of default arguments?
- One function definition can replace many overloads
- They make functions run faster
- They prevent null values
- They are required on every parameter
Answer: One function definition can replace many overloads. Defaults let one function serve many call patterns without overloading.
What does a named argument let you do that positional args cannot?
- Call a private function
- Change a parameter's type
- Add new parameters at runtime
- Skip earlier defaulted parameters to set only a later one
Answer: Skip earlier defaulted parameters to set only a later one. Named args can skip earlier defaults, e.g. makeBox(depth = 2).
Can you reorder arguments in a call using named arguments?
- No, order is always fixed
- Yes, named arguments can appear in any order
- Only the first two
- Only if all are named
Answer: Yes, named arguments can appear in any order. Named arguments may come in any order.
Mixing positional and named arguments: which rule applies?
- Once an argument is named, all following ones must also be named
- Named must come first
- You can never mix them
- Positional must come last
Answer: Once an argument is named, all following ones must also be named. f(1, y = 2) is fine, but f(x = 1, 2) is not.
When are default argument values evaluated?
- Once at definition time
- Never, they are constants
- On each call where the argument is omitted
- Only the first time the function runs
Answer: On each call where the argument is omitted. Defaults are evaluated per call, so System.currentTimeMillis() gives a fresh value each time.
What happens if you type a wrong parameter name in a named argument?
- It is silently ignored
- It won't compile
- It uses the default
- It picks the closest match
Answer: It won't compile. The named argument must match a declared parameter name exactly.
What does greet("Ada") output if greeting defaults to "Hello" and excited to false?
- Hello, Ada!
- Ada
- Hi, Ada.
- Hello, Ada.
Answer: Hello, Ada.. Both defaults are used: greeting Hello and excited false gives a period.
Why do Java callers not see Kotlin default arguments automatically?
- Java cannot call Kotlin
- Java needs @JvmOverloads to generate overloads
- Defaults are private
- Java ignores all annotations
Answer: Java needs @JvmOverloads to generate overloads. Within Kotlin defaults just work; Java interop needs @JvmOverloads.
Can a default value reference an earlier parameter of the same function?
- No, never
- Only constants are allowed
- Yes, a later default may use an earlier parameter
- Only the first parameter
Answer: Yes, a later default may use an earlier parameter. Defaults can reference earlier parameters of the same function.
Continue this course
- Previous: Functions
- Next: Collections (List, Set, Map)