How to Write swap Function in Kotlin using the also Function?
Last Updated :
21 Jun, 2025
Swapping two numbers is one of the most common tasks in programming. Usually, when we swap two values in languages like Java or C++, we either use a third temporary variable or pointers/references to perform the swap. In Java, since we don’t have pointers, we almost always rely on a third variable to swap two numbers:
int temp = a;
a = b;
b = temp;
This is simple and works perfectly but Kotlin offers a shorter, more elegant way to swap values without using any third variable. Let’s learn how!
The also Function in Kotlin -
Before we jump into the swap example, we need to understand the also function in Kotlin. The also function is an extension function available to all objects. It allows us to perform some operations on an object and then returns the same object.
Let’s look at the basic usage:
val dog = Dog(12).also {
// 'it' refers to the object itself
it.age = 13
}
Here we have an object Dog(12). We used also to change its age to 13. The also function returns the same modified object.
The Swap Example Using also
Here’s how we can swap two numbers in Kotlin without using a third variable by using the also function.
Kotlin
fun main() {
var a = 5
var b = 10
a = b.also { b = a }
println("a = $a")
println("b = $b")
}
Output:
a = 10
b = 5
Let’s understand this step by step:
- b.also { b = a } returns the original value of b, but before returning, it sets b = a.
- The returned value (original b) is assigned to a.
- Now the values are swapped, and we did this without needing a third variable.
Now, we must wonder why does it work. Let's think of an expression.
a = b.also { b = a }
The function also takes the value of b (which is 10). Inside the block { b = a }, we assign b = a (which is 5). So now b = 5. The original value of b (10) is returned by also, and this value gets assigned to a. Result: a = 10, b = 5. The numbers have swapped without using a temporary variable.
Difference Between also and apply
Many people often confuse the also and apply functions in Kotlin because they seem similar, but there is an important difference in how they handle the receiver object inside their lambda blocks. In the case of also, the receiver is accessed using the keyword it, and the function always returns the same receiver object after performing the specified operations. On the other hand, apply uses this as the receiver inside its block, and in fact, this is implicit, so you can refer to the object's properties and functions directly without needing to use any special keyword. Just like also, apply also returns the same receiver object after applying the changes.
Example with also function:
Kotlin
val dog = Dog(12).also {
// must use 'it'
it.age = 13
}
Example with apply function:
Kotlin
val dog = Dog(12).apply {
// 'this' is implicit
age = 13
}
Both modify the same object and return the object itself, but in also, you must use it to refer to the object. In apply, you can use this (or nothing at all since it's implicit).
Similar Reads
How to Specify Default Values in Kotlin Functions? In Kotlin, you can provide default values to parameters in a function definition. If the function is called with arguments passed, those arguments are used as parameters. However, if the function is called without passing argument(s), default arguments are used. But If you come from the Java world,
4 min read
Transform a List Using Map Function in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Working with Anonymous Function in Kotlin In Kotlin, we can treat functions as expressions by creating lambdas. A lambda is a function literal, meaning itâs a function that doesnât have a name and is defined directly as an expression. These lambda expressions can also be passed as parameters to other functions. However, one important thing
3 min read
Passing Variable Arguments to a Function in Kotlin There are a lot of scenarios in which we need to pass variable arguments to a function. In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T
4 min read
How to Pass a Function as a Parameter to Another in Kotlin? In Kotlin, we have the amazing power to work with higher-order functions. These are special kinds of functions that can take other functions as parameters or even return functions as results. This feature makes our code more flexible, cleaner, and easier to understand. In fact, many useful functions
3 min read