androidengineers.Book a session
โ† All interview questions
KotlinIntermediate4 min

What is inline function in Kotlin?

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

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

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

Compiler output (conceptually):

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.

Mark this when you can explain the answer in your own words.

Share & Help Others

Help fellow developers prepare for interviews

Sharing helps the Android community grow ๐Ÿ’š

Keep practising