Skip to main content
Browse topics

noinline and crossinline in Kotlin

2 min read Updated June 30, 2026
Share

Introduction

noinline and crossinline is a fundamental concept every Kotlin developer should understand. Higher-order functions accept or return other functions, enabling reusable patterns like map, filter, and custom control abstractions.

Noinline prevents a lambda parameter from being inlined. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • noinline prevents a lambda parameter from being inlined.
  • crossinline marks lambda that cannot use non-local return.
  • These modifiers control inline behavior in advanced cases.

Syntax

kotlin
inline fun demo(crossinline block: () -> Unit)

noinline and crossinline in Kotlin Example Program in Kotlin

kotlin
inline fun runTask(crossinline task: () -> Unit) {
    println("Before task")
    task()
    println("After task")
}

fun main(args: Array<String>) {
    runTask { println("Task running") }
}

Sample Output

plaintext
Before task
Task running
After task

When to use

Use higher-order functions to extract repeated control-flow patterns into reusable abstractions.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. The println("Before task") statement writes a line to the console — this produces part of the sample output below.

  3. The println("After task") statement writes a line to the console — this produces part of the sample output below.

  4. The println("Task running") statement writes a line to the console — this produces part of the sample output below.

  5. Noinline prevents a lambda parameter from being inlined.

  6. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

Common Mistakes

Key Points

  • noinline prevents a lambda parameter from being inlined.
  • crossinline marks lambda that cannot use non-local return.
  • These modifiers control inline behavior in advanced cases.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is noinline and crossinline in Kotlin?
Noinline prevents a lambda parameter from being inlined.
When should I use noinline and crossinline?
Use higher-order functions to extract repeated control-flow patterns into reusable abstractions.
How is noinline and crossinline different from Java?
These modifiers control inline behavior in advanced cases.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related tutorials

Search tutorials