# Using Scoped Functions in Kotlin - let, run, with, also, apply

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679480384426/279c3545-c0b8-49c3-bf6e-59f00aad57e6.png align="center")

Now that almost all Android developers have switched from Java to Kotlin, they are finding how much easier, cleaner, and more concise Kotlin is than Java. Kotlin introduces a number of developer-friendly features for less code, reducing the number of problems.

Kotlin is a modern, concise and powerful programming language that has gained immense popularity in recent years. One of the reasons for its success is its support for functional programming concepts. In this blog, we will explore important higher-order functions in Kotlin: let, run, also and apply.

## **The let function**

The let function is a higher-order function that takes a lambda expression as an argument and executes it with the value of the calling object as its argument. The result of the lambda expression is then returned. Let's take a look at an example:

```kotlin
val name: String? = "John Doe"

val result = name?.let {
    "Hello, $it!"
} ?: "Unknown"

println(result) // Output: Hello, John Doe!
```

In this example, we use the safe call operator to ensure that the let function is only called if the `name` variable is not null. The lambda expression in the let function concatenates the string "Hello, " with the value of `it`, which is the non-null value of `name`. If `name` is null, the `?:` operator returns the default value of "Unknown".

## **The run function**

The run function is similar to the let function in that it takes a lambda expression as an argument and executes it with the value of the calling object as its argument. However, the difference is that the run function does not return the result of the lambda expression, but instead returns the calling object itself. Here's an example:

```kotlin
val numbers = mutableListOf<Int>(1, 2, 3, 4)

val sum = numbers.run {
    add(5)
    add(6)
    sum()
}

println(sum) // Output: 21
```

In this example, we use the run function to add two numbers to the `numbers` list and then calculate the sum of all the elements in the list using the `sum()` function. The result of the `sum()` function is returned by the run function, but since it doesn't matter in this case, we ignore it.

## **The also function**

The also function is similar to the let function in that it takes a lambda expression as an argument and executes it with the value of the calling object as its argument. However, the difference is that the also function does not return the result of the lambda expression, but instead returns the calling object itself. Here's an example:

```kotlin
val message = "Hello, World!"

val result = message.also {
    println(it)
}.length

println(result) // Output: 13
```

In this example, we use the also function to print the value of the `message` variable and then return its length. The `it` parameter in the lambda expression refers to the value of the `message` variable. Since the also function returns the calling object, we can chain other functions after it.

## **The apply function**

The apply function is similar to the run function in that it takes a lambda expression as an argument and executes it with the value of the calling object as its argument. However, the difference is that the apply function returns the calling object itself, rather than the result of the lambda expression. Here's an example:

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

val person = Person("John Doe", 30).apply {
    age += 1
}

println(person.age) // Output: 31
```

In this example, we use the apply function to create a `Person` object with the name "John Doe" and the age 30. We then use the apply function to increment the age of the `person` object by 1. Since the apply function returns

All there is to know about Kotlin's Scoped functions. We hope that our blog has provided you with a greater understanding of and a method for constructing the proper scoped function in the right place, even though we may be using this in our code.

I greatly thank you for your time.

*If you want to read more content like this, please go check out my profile.*

@[Mansi Vaghela](@mansivaghela) [**LinkedIn**](https://www.linkedin.com/in/mansi-droid/) [**Twitter**](https://twitter.com/mansi_droid) [**Github**](https://github.com/mansi-droid) [**Youtube**](https://www.youtube.com/channel/UCoGcTjvUp32To72hQVe7iZw)
