inline function in Kotlin
Tags: android, kotlin
Published: 2023-01-10 13:26:49

---
title: inline function in Kotlin
published: true
description: In this blog, we will learn about the inline function in Kotlin.
tags: android, kotlin
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1grn5bcbeyjjbqsox3w.png
canonical_url: https://amitshekhar.me/blog/inline-function-in-kotlin
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out [Android Interview Questions and Answers](https://www.youtube.com/playlist?list=PL_I3TGB7aK6jNBMZkw3FYdJXyf7quHdI8).

In this blog, we will learn about the `inline` function in Kotlin.

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/inline-function-in-kotlin).**

## 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:

```kotlin
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 `Decompile` button to get your Java code from the bytecode.

We will get the following:

```java
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.

```kotlin
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:

```java
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.**

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.

```kotlin
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:

```java
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.

```kotlin
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:

```java
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.

Learn about noinline: [noinline in Kotlin](https://amitshekhar.me/blog/noinline-in-kotlin)

Learn about crossinline: [crossinline in Kotlin](https://amitshekhar.me/blog/crossinline-in-kotlin)

Watch the video format: [inline function in Kotlin](https://www.youtube.com/watch?v=GLLI8h67ryo)

Master Kotlin Coroutines from here: [Mastering Kotlin Coroutines](https://amitshekhar.me/blog/kotlin-coroutines)

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)

[**Read all of my high-quality blogs here.**](https://amitshekhar.me/blog)