What if you could write one function that works perfectly in many situations without extra hassle?
Why Parameters with default values in Kotlin? - Purpose & Use Cases
Imagine you have a function that greets users. Sometimes you want to greet by name, other times just a simple hello. Without default values, you must write many versions of the same function or always provide all details.
Writing many function versions is tiring and error-prone. You might forget to update all versions when changing logic. Also, calling functions with many parameters every time is slow and confusing.
Parameters with default values let you write one function that works in many ways. You can skip parameters you don't want to change, making your code cleaner and easier to read.
fun greet(name: String) { println("Hello, $name!") }
fun greet() { println("Hello!") }fun greet(name: String = "there") { println("Hello, $name!") }
You can write simpler, flexible functions that adapt to different needs without extra code.
When making a messaging app, you can create a sendMessage function with default parameters for things like font size or color, so users get a nice message even if they don't customize every detail.
Default parameters reduce repetitive code.
They make functions easier to call and maintain.
They help your code stay clean and flexible.