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.

Write one log function with two defaulted parameters, then call it three different ways using named arguments.

📋 Quick Reference — Arguments

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.