First-class functions let Kotlin treat behavior as a value: store it in a variable, pass it to another function, or return it from a function. This makes it practical to separate shared logic from the behavior that varies, reducing duplication and enabling higher-level abstractions. section 1.2.3 section 5.1.1
Pass behavior directly
A click listener needs behavior for its onClick method. An object declaration can supply that behavior, but a lambda expresses only the action needed: button.setOnClickListener { println("I was clicked!") } The lambda is a block of code used as a function argument. It replaces repeated anonymous listener-object boilerplate with a concise expression of what happens on a click. section 5.1.1
This is more than shorter listener syntax. When similar operations differ only in one step, extract the common operation and pass the differing behavior as a function. Lambdas provide concise notation for those function values. Combined with immutable values and functions without side effects, this style can improve isolation for testing and reduce unsafe shared-state changes in concurrent code. section 1.2.3 section 5.1.1
messages
.filter { it.body.isNotBlank() && !it.isRead }
.map(Message::sender)
.distinct()
.sortedBy(Sender::name)
This collection pipeline composes operations as behavior: it filters unread nonblank messages, maps them to senders, removes duplicates, and sorts by sender name. section 1.2.3
Return behavior chosen at runtime
A function can also produce another function. getShippingCostCalculator selects a calculator from Delivery, returns it as an (Order) -> Double value, and the caller later invokes that value with an Order. The returned function keeps pricing choice separate from the code that applies the calculation. The same pattern can return a contact predicate: build a function from filter settings, then pass it to filter. This decouples contact-display logic from the UI state that chooses which contacts qualify. section 10.1.5 section 10.1.5
Use first-class functions when behavior should be selected, supplied, or reused independently of an operation:
- Pass a lambda as a callback, such as a button click handler. section 5.1.1
- Extract a varying step from repeated logic and provide it as a function argument. section 1.2.3
- Return a calculator or predicate when configuration determines later behavior. section 10.1.5






