Cheat Sheets

Kotlin Functions & Lambdas Cheat Sheet: Defaults, Higher-Order & Inline

By ArpaNeuro Team September 18, 2026
Kotlin functions cheat sheet

Kotlin functions support default and named arguments, expressions as bodies, and lambdas as values. Higher-order functions take or return functions (map, your own retry). Trailing lambdas sit outside the parentheses. inline with reified recovers generic types that the JVM would erase.

This Kotlin cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.

Quick reference

  • Default: fun greet(name: String = "there").
  • Named: greet(name = "Humza") at the call site.
  • Single expr: fun square(n: Int) = n * n.
  • Lambda: val add = { a: Int, b: Int -> a + b } and it for one parameter.
  • Trailing: list.filter { it.active } — last lambda outside ().
  • Receiver: buildString { append("x") } is a lambda with receiver (this).
  • inline: Inlines the lambda to avoid allocation and allow reified / non-local return.
  • infix: infix fun String.should(other: String) for DSLs — easy to overuse.

Copy-paste examples

Higher-order retry and a trailing lambda

Keep public APIs named. Use inline only when you need reified types or you have measured a hot path.

inline fun <T> retry(times: Int, block: () -> T): T {
    var last: Throwable? = null
    repeat(times) {
        try {
            return block()
        } catch (t: Throwable) {
            last = t
        }
    }
    throw last ?: IllegalStateException("retry failed")
}

fun titles(rows: List<String>): List<String> =
    rows.filter { it.isNotBlank() }.map { it.trim() }

Common mistakes

  • Mutating a default listOf()-style default argument — use list: List<T> = emptyList() and do not mutate the default.
  • Making every helper inline and exploding bytecode.
  • Nested lambdas with multiple it bindings you cannot read later.
  • Java-style overloads of 8 methods instead of one defaulted function.

FAQ

  1. What is BLOGPH0? The implicit name of a single lambda parameter. Name it (item ->) when nested or unclear.
  1. Unit vs void? Kotlin functions return Unit by default. You can omit the type. Java void maps to Unit.
  1. When do I use BLOGPH0? When an inline function must pass a lambda to another non-inline API. The compiler will tell you.

Related Kotlin cheat sheets

Build with this stack

When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds mobile app development and also sells ready-made source code. Request a quote and tell us the stack.

Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.

← All Articles Get a Quote →