Enhance Your Function Calls with Inline Functions in Kotlin
inline functions are a powerful feature in Kotlin designed to enhance performance by reducing overhead associated with higher-order…

Enhance Your Function Calls with Inline Functions in Kotlin
inline functions are a powerful feature in Kotlin designed to enhance performance by reducing overhead associated with higher-order functions. Here’s everything you need to know about them
What is an Inline Function?
An inline function is a function in Kotlin where the compiler inserts the function’s bytecode directly into the call site, eliminating the need for a function call. This can lead to performance improvements, particularly in cases involving higher-order functions (functions that take other functions as parameters).
Benefits of Inline Functions
- Performance Improvement: By avoiding the overhead of a function call, inline functions can make your code run faster. This is especially noticeable in performance-critical sections of your code.
- Reduced Object Creation: Higher-order functions often create additional objects. Inline functions minimize this by eliminating unnecessary object creation.
- Simplified Bytecode: Inline functions can result in simpler bytecode, aiding the Just-In-Time (JIT) compiler in optimizing the code.
How to declare an inline function
Add the inline keyword at the beginning of the function definition.
inline fun startProcess(i: Int, block: () -> Unit) {
println("-------------------------Starting process number: $i--------------------------------")
block()
println("-------------------------Ending process number: $i---------------------------------\n\n")
}
Let’s understand with an example:
We have a function that processes numbers from 1 to 10. For each number, it calculates the sum of numbers up to 100 in steps of 2 and prints whether the sum.
fun main() {
for (i in 1..10) {
startProcess(i) {
val sum = calculateSum(i)
println("The process is: $i and sum is: $sum")
}
}
}
inline fun startProcess(i: Int, block: () -> Unit) {
println("-------------------------Starting process number: $i--------------------------------")
block()
println("-------------------------Ending process number: $i---------------------------------\n\n")
}
fun calculateSum(from: Int): Int {
var sum = 0
for (i in from..100 step 2) {
sum += i
}
return sum
}
Explanation:
- main(): Iterates from 1 to 10, calling
startProcess(i)for each number.. - startProcess(): Prints the start and end of the process, and runs the provided block of code.
- calculateSum(): Calculates the sum of numbers starting from
fromto 100 in steps of 2.
The code prints whether each number is even or odd and the sum of numbers up to 100 starting from that number.
Decompile the code and observe the differences in JVM.
We will demonstrate the difference in bytecode when using an inline function in Kotlin. First, we decompile the code without the inline keyword and observe the bytecode. Then, we add the inline keyword to the function and decompile it again to see how the bytecode changes.
- Open Kotlin file in IntelliJ IDEA.
- Go to
Tools>Kotlin>Show Kotlin Bytecode. - Click
Decompileto see the Java equivalent code.
Without inline keyword:

You can see that for every process, a new Function0() object is created. These objects are allocated in the heap memory, which can be expensive in terms of memory and performance. Now let’s findout what happen if we add inline keyowrd.
With inline keyword:

After adding the inline keyword, the function is directly copied into the calling function, eliminating the need for object creation. This optimizes memory usage and performance.
Advantages of Inline Functions
- Performance Improvement: Inline functions can reduce the overhead of function calls, especially useful for small functions and lambda expressions.
- Non-local Returns: Inline functions allow non-local returns, meaning you can return from the calling function rather than just the lambda itself.
- Reified Type Parameters: Inline functions can have reified type parameters, allowing you to use type parameters as real types at runtime.
Non-local Returns:
One of the key features of inline functions is the ability to return from the lambda directly:
inline fun inlineFunctionWithReturn(block: () -> Unit) {
println("Before block")
return block() // non-local return
println("After block") // This line will not be executed
}
fun main() {
inlineFunctionWithReturn {
println("Inside block")
return // Returns from main function
}
println("This will not be printed")
}
Reified Type Parameters:
Reified type parameters allow you to use the actual type within the function:
inline fun <reified T> printType() {
println(T::class)
}
fun main() {
printType<String>() // Prints: class kotlin.String
}
Noinline and Crossinline
When you do not want a particular lambda parameter to be inlined, you can mark it with the noinline keyword:
inline fun inlineFunction(noinline block: () -> Unit) {
println("Before calling block")
block()
println("After calling block")
}
If you want to ensure that a lambda cannot use non-local returns, you can mark it with the crossinline keyword:
inline fun inlineFunctionWithCrossinline(crossinline block: () -> Unit) {
println("Before calling block")
block()
println("After calling block")
}
fun main() {
inlineFunctionWithCrossinline {
println("Inside block")
// return // This will cause a compilation error
}
}
You can also mark them as crossinline and noinline
inline fun execute(crossinline action: () -> Unit, noinline callback: () -> Unit) {
// action will be inlined
// callback will not be inlined
}
Use Cases and Limitations
Use Cases
- Higher-Order Functions: Functions that take other functions as arguments benefit the most from inlining.
- Small Utility Functions: Functions that perform small tasks can be inlined to avoid function call overhead.
Limitations
- Code Size Increase: Excessive use of inlining can lead to code bloat, as the function body is copied everywhere the function is called.
- Complex Functions: Inline functions should be simple and small. Complex functions with large bodies are not ideal for inlining.
Summary
Inline functions in Kotlin are a powerful feature for optimizing higher-order functions and allowing non-local returns and reified type parameters. They help reduce the overhead of function calls, making the code more efficient. However, they should be used judiciously to avoid increasing the code size unnecessarily.
For more detailed information, refer to the **official Kotlin documentation**.
메타데이터
- post_id
- dfe1d938648d
- slug
- enhance-your-function-calls-with-inline-functions-in-kotlin-dfe1d938648d
- url
- https://medium.com/@mahbubmridha07/enhance-your-function-calls-with-inline-functions-in-kotlin-dfe1d938648d
- canonical_url
- https://medium.com/@mahbubmridha07/enhance-your-function-calls-with-inline-functions-in-kotlin-dfe1d938648d
- author_url
- https://medium.com/@mahbubmridha07
- status
- ok
- fetched_at
- 2026-06-27 18:20:27