# Extension functions In this chapter, you'll explore special Kotlin functions that make your code more concise and readable. Learn how they can help you use efficient design patterns to take your projects to the next level. ## Extension functions In software development, you often need to modify a program's behavior without changing the original source code. For example, you might want to add extra functionality to a class from a third-party library. You can do this by adding extension functions to extend a class. You call extension functions the same way you call member functions of a class, using a period `.`. Before introducing the complete syntax for extension functions, you need to understand what a receiver is. The receiver is what the function is called on. In other words, the receiver is where or with whom the information is shared. ![An example of sender and receiver](images/receiver-highlight.png) In this example, the `main()` function calls the [.first()](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/first.html) function to return the first element in a list. The `.first()` function is called on the `readOnlyShapes` variable, so the `readOnlyShapes` variable is the receiver. To create an extension function, write the name of the class that you want to extend followed by a `.` and the name of your function. Continue with the rest of the function declaration, including its parameters and return type. For example: ```KOTLIN fun String.bold(): String = "$this" fun main() { // "hello" is the receiver println("hello".bold()) // hello } ``` In this example: * `String` is the extended class. * `bold` is the name of the extension function. * The `.bold()` extension function's return type is `String`. * `"hello"`, an instance of `String`, as the receiver. * The receiver is accessed inside the body by the [keyword](keyword-reference.html): `this`. * A string template (`$`) is used to access the value of `this`. * The `.bold()` extension function takes a string and returns it in a `` HTML element for bold text. ## Extension-oriented design You can define extension functions anywhere, which enables you to create extension-oriented designs. These designs separate core functionality from useful but non-essential features, making your code easier to read and maintain. A good example is the [HttpClient](https://api.ktor.io/ktor-client-core/io.ktor.client/-http-client/index.html) class from the Ktor library, which helps perform network requests. The core of its functionality is a single function `request()`, which takes all the information needed for an HTTP request: ```KOTLIN class HttpClient { fun request(method: String, url: String, headers: Map): HttpResponse { // Network code } } ``` In practice, the most popular HTTP requests are GET or POST requests. It makes sense for the library to provide shorter names for these common use cases. However, these don't require writing new network code, only a specific request call. In other words, they are perfect candidates to be defined as separate `.get()` and `.post()` extension functions: ```KOTLIN fun HttpClient.get(url: String): HttpResponse = request("GET", url, emptyMap()) fun HttpClient.post(url: String): HttpResponse = request("POST", url, emptyMap()) ``` These `.get()` and `.post()` functions extend the `HttpClient` class. They can directly use the `request()` function from the `HttpClient` class because they're called on an instance of the `HttpClient` class as the receiver. You can use these extension functions to call the `request()` function with the appropriate HTTP method, which simplifies your code and makes it easier to understand: ```KOTLIN class HttpClient { fun request(method: String, url: String, headers: Map): HttpResponse { println("Requesting $method to $url with headers: $headers") return HttpResponse("Response from $url") } } fun HttpClient.get(url: String): HttpResponse = request("GET", url, emptyMap()) fun main() { val client = HttpClient() // Making a GET request using request() directly val getResponseWithMember = client.request("GET", "https://example.com", emptyMap()) // Making a GET request using the get() extension function // The client instance is the receiver val getResponseWithExtension = client.get("https://example.com") } ``` This extension-oriented approach is widely used in Kotlin's [standard library](https://kotlinlang.org/api/latest/jvm/stdlib/) and other libraries. For example, the `String` class has many [extension functions](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/#extension-functions) to help you work with strings. For more information about extension functions, see [Extensions](extensions.html). ## Practice Check whether an integer is positive : Write an extension function called `isPositive` that takes an integer and checks whether it is positive. : ```KOTLIN fun Int.// Write your code here fun main() { println(1.isPositive()) // true } ``` : ```KOTLIN fun Int.isPositive(): Boolean = this > 0 fun main() { println(1.isPositive()) // true } ``` Convert a string to lowercase : Write an extension function called `toLowercaseString` that takes a string and returns a lowercase version. : Hint : Use the : [.lowercase()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/lowercase.html) : function for the : `String` : type. : ```KOTLIN fun // Write your code here fun main() { println("Hello World!".toLowercaseString()) // hello world! } ``` : ```KOTLIN fun String.toLowercaseString(): String = this.lowercase() fun main() { println("Hello World!".toLowercaseString()) // hello world! } ``` ## See also * [Previous step](kotlin-tour-null-safety.html) * [Next step](kotlin-tour-intermediate-scope-functions.html)