Skip to main content
St Louis

Back to all posts

How to Create And Call an Extension Function In Kotlin?

Published on
5 min read
How to Create And Call an Extension Function In Kotlin? image

Best Kotlin Programming Guides to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
3 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
4 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
5 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
6 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
7 Atomic Kotlin

Atomic Kotlin

BUY & SAVE
$44.91 $49.00
Save 8%
Atomic Kotlin
8 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
9 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
+
ONE MORE?

To create and call an extension function in Kotlin, follow the steps below:

  1. Create a new Kotlin file or open an existing one.
  2. Define the extension function outside of any class, using the fun keyword followed by the function name. Prefix the defining type with the class or interface that the function extends. For example, if you want to create an extension function for the String class, you would write fun String.myExtensionFunction().
  3. Specify the function body by adding curly braces {} after the function signature.
  4. Inside the function body, you can use the this keyword to refer to the object on which the function is being called. This object will have access to all the properties and functions of the extended class.
  5. Once the extension function is defined, you can call it on any object that belongs to the extended class, as if it were a regular member function. The extension function will appear in the IDE's suggestions and can be called using the regular function call syntax.

Here's an example illustrating the creation and usage of an extension function that capitalizes the first letter of a string:

// Extension function fun String.capitalizeFirstLetter(): String { if (isEmpty()) return this return this.substring(0, 1).toUpperCase() + this.substring(1) }

// Usage fun main() { val input = "hello, world!" val capitalized = input.capitalizeFirstLetter()

println(capitalized) // Output: Hello, world!

}

In this example, the extension function capitalizeFirstLetter() is added to the String class. It takes a string, checks if it's empty, capitalizes the first letter (if not empty), and returns the modified string. Finally, the extension function is called on a string object named input, and the output is printed to the console.

What is the scope of an extension function in Kotlin?

The scope of an extension function in Kotlin is limited to the class it is defined in. It cannot access private members or properties of the class, and it cannot be overridden by subclasses. The extension function can be called on instances of the class just like any other member function. Additionally, the extension function can be accessed within the class it is defined in using the this keyword.

How to group multiple extension functions in a single file in Kotlin?

To group multiple extension functions in a single file in Kotlin, follow these steps:

  1. Create a Kotlin file and give it a relevant name. For example, if the extension functions are related to string manipulation, you could name the file StringExtensions.kt.
  2. Define the extension functions in the file. Each extension function should be defined using the fun keyword followed by the receiver type it extends and the name of the function. Inside the extension function, you can access the receiver object using this.

Here's an example of a StringExtensions.kt file with two extension functions:

// StringExtensions.kt

fun String.isPalindrome(): Boolean { val reversed = this.reversed() return this == reversed }

fun String.capitalizeWords(): String { return this.split(" ").joinToString(" ") { it.capitalize() } }

In this example, the isPalindrome() function extends the String type and checks whether the string is a palindrome. The capitalizeWords() function extends the String type and capitalizes the first letter of each word in the string.

  1. Now you can use these extension functions by importing the file where you defined them. Since Kotlin automatically imports files based on the file name, you don't need any additional imports. Simply use the extension functions directly on string objects:

fun main() { val name = "kayak" println(name.isPalindrome()) // Output: true

val sentence = "hello world"
println(sentence.capitalizeWords()) // Output: Hello World

}

By following these steps, you can group multiple extension functions in a single file and easily reuse them throughout your application.

How to create an extension function that modifies the receiver object in Kotlin?

To create an extension function that modifies the receiver object, you can define the extension function with the receiver object as a parameter and modify its properties or call its methods within the function. Here's an example:

class Person(var name: String, var age: Int)

fun Person.greet() { name = "Hello, $name!" }

fun main() { val person = Person("John", 25) person.greet()

println(person.name) // Output: Hello, John!
println(person.age) // Output: 25

}

In this example, we define an extension function greet() for the Person class. This function modifies the name property of the receiver object by appending "Hello, " before the original name. The greet() function is called on a Person object, so it modifies the name property of that object.

When we invoke the greet() function on the person object, it modifies the name property to "Hello, John!". We can then access the modified name property using person.name and it will print "Hello, John!".

How to specify the receiver type in an extension function?

To specify the receiver type in an extension function, you need to define the receiver type before the function name. Here's the syntax:

fun ReceiverType.functionName() { // Extension function implementation }

Here, ReceiverType is the type of the object for which you want to create an extension function. You can then use the this keyword inside the function body to refer to the object on which the extension function is called.

For example, if you want to create an extension function on the String class that prints the length of the string, you can do it like this:

fun String.printLength() { println("Length of the string is ${this.length}") }

Now, you can call this extension function on any string object:

val str = "Hello, World!" str.printLength() // Output: Length of the string is 13

In the example above, String is the receiver type, and printLength is the extension function on that type.