🚀 Enrollments Open for Jetpack Compose Cohort 2 — 7 Days of Live Learning to Build Modern Android UIs 💚Join Now
KotlinIntermediate4 min
What is inline function in Kotlin?

Recommended Resources

Answer

An inline function instructs the compiler to copy the function's code and paste it directly into the call site, instead of creating a standard function call object.

Why use it?

  • Performance: Reduces overhead of function calls and object allocation, especially for Higher-Order Functions (functions that take lambdas).
  • Without `inline`, every lambda passed to a function creates an anonymous class object in Java bytecode, which costs memory.

Example

```kotlin inline fun executeOp(op: () -> Unit) { println("Start") op() println("End") }

// Usage executeOp { println("Working") } ```

Compiler output (conceptually): ```kotlin println("Start") println("Working") // The lambda body is pasted here println("End") ```

Caveat

Don't inline large functions, as it increases the bytecode size of your application.

Want to master these concepts?

Join our live cohorts and build production-ready Android apps.

Accelerate Your Growth

Don't just learn concepts in isolation. Build production-ready Android apps with expert guidance.

Live Interactive Sessions
Code Reviews & Feedback
Real-world Projects
Career Guidance

Limited seats available for next cohort