-
Notifications
You must be signed in to change notification settings - Fork 22
Kotlin: Inline Functions
Devrath edited this page Dec 25, 2023
·
2 revisions
![Screenshot 2023-12-25 at 11 43 43β―PM](https://private-user-images.githubusercontent.com/1456191/292774027-cf7e396b-5db8-4f99-8548-9a13319da42b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ2MTIyNDEsIm5iZiI6MTcyNDYxMTk0MSwicGF0aCI6Ii8xNDU2MTkxLzI5Mjc3NDAyNy1jZjdlMzk2Yi01ZGI4LTRmOTktODU0OC05YTEzMzE5ZGE0MmIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMTg1MjIxWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ODJkNWFmMzRiZDMyZTQ4MmZkZGQ0NzNkNTA3ZWQwZTVhZWUwZDkzODkyYzJlNDYwYTM2NGQyMWFkODBhM2U2OSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.BH1DqLEK8-hUvxF7frDlh2_kqrZ0zx4AQzBUQuHyEuo)
- 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 theinline
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.
fun guide() {
println("Start")
SomeAction()
println("End")
}
inline fun SomeAction(){
println("Action Performed")
}
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 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.