0
0
Kotlinprogramming~10 mins

Also function behavior and use cases in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Also function behavior and use cases
Start with an object
Call also { block }
Inside block: 'it' refers to object
Perform side actions (e.g., print, modify)
Return original object
Continue with original object
The also function lets you run extra code on an object and then returns the original object unchanged.
Execution Sample
Kotlin
val number = 5
val result = number.also {
    println("Number is $it")
}.times(2)
println(result)
Prints the number using also, then multiplies it by 2 and prints the result.
Execution Table
StepCode LineActionValue of 'it'Result/Output
1val number = 5Assign 5 to number-number = 5
2number.also { println("Number is $it") }Call also on number; inside block print5Prints: Number is 5
3.times(2)Multiply original number by 2-Result = 10
4println(result)Print final result-Prints: 10
5-End of execution--
💡 Execution stops after printing the final result 10.
Variable Tracker
VariableStartAfter also blockAfter times(2)Final
number5555
result--1010
Key Moments - 2 Insights
Why does the also function return the original object and not the block's last expression?
Because also is designed to perform side effects without changing the object; see execution_table step 2 where the block prints but the original number is returned.
What does 'it' refer to inside the also block?
'it' refers to the original object the also function is called on, as shown in execution_table step 2 where 'it' is 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is printed inside the also block?
ANumber is 10
BNumber is 5
CResult is 10
DNo output
💡 Hint
Check the 'Result/Output' column at step 2 in execution_table.
At which step does the variable 'result' get the value 10?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Value of result' in variable_tracker after times(2).
If the also block modified 'it' to 7, what would be the value of 'result' after times(2)?
A14
B7
C10
D5
💡 Hint
also returns the original object unchanged, see key_moments about return behavior.
Concept Snapshot
also function syntax:
object.also { it -> /* side actions */ }

- Runs block with 'it' as the object
- Returns original object unchanged
- Useful for side effects like logging or debugging
- Allows chaining without breaking flow
Full Transcript
The also function in Kotlin lets you run extra code on an object without changing it. When you call also on an object, it passes the object as 'it' inside the block. You can do things like print or modify properties inside the block. After the block runs, also returns the original object unchanged. This is useful when you want to add side effects like logging but keep using the original object in a chain of calls. For example, number.also { println("Number is $it") }.times(2) prints the number and then multiplies it by 2. The execution steps show the value of variables and outputs at each stage, helping you see how also works step-by-step.