inline function in Kotlin
- Authors
- Name
- Amit Shekhar
- Published on
An inline function in Kotlin instructs the compiler to insert the complete body of the function wherever that function gets used in the code.
In this blog, we will learn about the inline function in Kotlin.
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning, and Android at Outcome School.
Join Outcome School and get a high-paying tech job:
Let's get started.
What is an inline function in Kotlin?
Inline function instruct compiler to insert the complete body of the function wherever that function gets used in the code.
Let's understand it with an example:
fun guide() {
print("guide start")
teach()
print("guide end")
}
fun teach() {
print("teach")
}
In this example, we have two functions:
guide()teach()
Both are normal functions. guide() function calls the teach() function.
Let's see the decompiled code in order to understand it.
For that, we will have to convert this Kotlin source file to a Java source file.
Steps to convert from Kotlin source file to Java source file and decompile in Android Studio:
Tools>Kotlin>Show Kotlin Bytecode. You will get the bytecode of your Kotlin file.- Now click on the
Decompilebutton to get your Java code from the bytecode.
We will get the following:
public void guide() {
System.out.print("guide start");
teach();
System.out.print("guide end");
}
public void teach() {
System.out.print("teach");
}
Here, we can see that the guide() function calls the teach() function as usual.
Now let's add the inline keyword to the teach() function.
fun guide() {
print("guide start")
teach()
print("guide end")
}
inline fun teach() {
print("teach")
}
Again, let's see the decompiled code. The decompiled code is as below:
public void guide() {
System.out.print("guide start");
System.out.print("teach");
System.out.print("guide end");
}
Now, we can see that the code of teach() function is copied inside the guide() function. And the guide() function is no more calling the teach() function.
This is because we have used the inline keyword.
Advantage of inline function: Function call overhead doesn't occur. Less overhead and faster program execution.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
So, when to make the function inline and when not:
- When the function code is very small, it's a good idea to make the function
inline. - When the function code is large and called from so many places, it's a bad idea to make the function
inline, as the large code will be repeated again and again.
Now, let's take an example with Higher-Order Function and Lambdas.
fun guide() {
print("guide start")
teach {
print("teach")
}
print("guide end")
}
fun teach(abc: () -> Unit) {
abc()
}
Again, let's go to the decompiled code. The decompiled code is as below:
public void guide() {
System.out.print("guide start");
teach(new Function() {
@Override
public void invoke() {
System.out.print("teach");
}
});
System.out.print("guide end");
}
public void teach(Function abc) {
abc.invoke();
}
Now let's add the inline keyword to the teach() function.
fun guide() {
print("guide start")
teach {
print("teach")
}
print("guide end")
}
inline fun teach(abc: () -> Unit) {
abc()
}
Again, let's go to the decompiled code. The decompiled code is as below:
public void guide() {
System.out.print("guide start");
System.out.print("teach");
System.out.print("guide end");
}
As we can see that the code of teach() function is placed inside the guide() function.
This is how inline can help us to make the execution fast by avoiding the function calls.
Now, we have understood the inline keyword in Kotlin.
To go deep into Kotlin topics like inline, noinline, crossinline, higher-order functions, and lambdas, check out the Android Program by Outcome School.
Learn about noinline: noinline in Kotlin
Learn about crossinline: crossinline in Kotlin
Watch the video format: inline function in Kotlin
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Frequently Asked Questions
Should we make every function inline in Kotlin?
No. When the function code is very small, it's a good idea to make it inline. But when the function code is large and called from many places, it's a bad idea, as the large code will be repeated again and again wherever the function is used.
Does an inline function make the program faster?
Yes. With an inline function, the function call overhead doesn't occur. The compiler copies the body of the function into the place where it is called. So, we get less overhead and faster program execution.
What happens to a lambda passed to an inline function?
The lambda code gets placed directly inside the calling function. Without inline, the decompiled code creates a new Function object for the lambda and passes it to the function. With inline, there is no such object and no function call, just the code itself.
How can we check if a function is actually inlined?
We can check the decompiled code in Android Studio. We go to Tools, then Kotlin, then Show Kotlin Bytecode, and click the Decompile button to get the Java code. If the function is inlined, its body appears inside the caller instead of a function call.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
