0
0
Kotlinprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Run function behavior and use cases
Call run on object
Execute lambda with 'this' as receiver
Lambda body runs using object's context
Return lambda result
Use returned value or end
The run function executes a block of code with the object as its context and returns the block's result.
Execution Sample
Kotlin
val result = "Hello".run {
    println(this)
    length
}
Runs a block on the string "Hello", prints it, and returns its length.
Execution Table
StepActionEvaluationResult
1Call run on "Hello"run block starts with 'this' = "Hello"Lambda ready to execute
2Print thisPrints "Hello" to consoleOutput: Hello
3Evaluate length"Hello".length5
4Return lengthrun returns 5result = 5
5EndNo more codeExecution stops
💡 run block finished and returned length 5
Variable Tracker
VariableStartAfter run callAfter lambda executionFinal
thisN/A"Hello""Hello"N/A
resultN/AN/A55
Key Moments - 3 Insights
Why does 'this' inside run refer to the object?
Because run executes the lambda with the object as the receiver, so 'this' points to the object (see execution_table step 1).
What does run return?
It returns the last expression value inside the lambda block (see execution_table step 4 where it returns 5).
Can run be used to execute multiple statements on an object?
Yes, run allows multiple statements inside the lambda using the object's context (see execution_table steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after the run block finishes?
Anull
B"Hello"
C5
Dlength of lambda
💡 Hint
Check execution_table row 4 where run returns 5 assigned to result.
At which step does the lambda print the string?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 2 where println(this) happens.
If we replaced 'length' with 'this.uppercase()', what would run return?
A"HELLO"
B5
CUnit
DError
💡 Hint
run returns the last expression value inside the lambda (see execution_table step 4).
Concept Snapshot
run function syntax:
object.run { /* lambda with 'this' as receiver */ }

Behavior:
- Executes lambda with object as 'this'
- Returns lambda's last expression

Use cases:
- Access object members concisely
- Execute multiple statements on object
- Return computed result from block
Full Transcript
The run function in Kotlin lets you run a block of code with an object as its context, meaning inside the block 'this' refers to the object. When you call run on an object, it executes the lambda you provide, and the last expression inside that lambda is returned as the result. For example, calling run on the string "Hello" and printing 'this' inside prints Hello, then returning length returns 5. Variables 'this' and 'result' change accordingly during execution. Beginners often wonder why 'this' refers to the object, what run returns, and if multiple statements can be used inside run. The answers are that run uses the object as receiver, returns the last lambda expression, and supports multiple statements. Visual quizzes help reinforce these points by asking about the result value, when printing happens, and what happens if the last expression changes. The quick snapshot summarizes syntax, behavior, and use cases for easy recall.