Skip to content

Kotlin: Inline Functions

Devrath edited this page Dec 25, 2023 · 2 revisions
Screenshot 2023-12-25 at 11 43 43β€―PM

What does an inline function do

  • Basically, a function can be called using a function call as seen above in normal-function when the control flow jumps from the main function to the called function and then the control jumps back to the calling function.
  • In the inline-function just by marking the called function with the inline keyword, the call is removed since during the compile time the entire called function is cut and pasted in the called position as seen in the right hand side of the diagram.

Code

fun guide() {
  println("Start")
  SomeAction()
  println("End")
}

inline fun SomeAction(){
   println("Action Performed")
}

When to make functions inline

Say there is a function that is small meaning the body of the function is not too big, That time we can use inline functions.

When not to use the inline function

When the body of the function is large and it is used in a lot of places, It is not a good idea to use an inline function since a large code block will be repeated in a lot of places.

Clone this wiki locally