0
0
Kotlinprogramming~10 mins

It keyword for single parameter in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - It keyword for single parameter
Start Lambda
Single Parameter?
Yes
Use 'it' as parameter
Execute Lambda Body
Return Result
End
When a lambda has one parameter, Kotlin lets you use 'it' to refer to it without naming it explicitly.
Execution Sample
Kotlin
val numbers = listOf(1, 2, 3)
val doubled = numbers.map { it * 2 }
println(doubled)
This code doubles each number in the list using 'it' as the single lambda parameter.
Execution Table
StepLambda Input (it)OperationResultOutput List State
111 * 22[2]
222 * 24[2, 4]
333 * 26[2, 4, 6]
4-End of list-[2, 4, 6]
💡 All elements processed, lambda applied to each using 'it', resulting list created.
Variable Tracker
VariableStartAfter 1After 2After 3Final
it-123-
doubled[][2][2, 4][2, 4, 6][2, 4, 6]
Key Moments - 3 Insights
Why can we use 'it' without declaring a parameter name?
Because the lambda has exactly one parameter, Kotlin automatically provides 'it' as its name (see execution_table steps 1-3).
What if the lambda had more than one parameter?
Then you must name the parameters explicitly; 'it' only works for single-parameter lambdas (not shown in this example).
Is 'it' a keyword or a variable?
'it' is an implicit name Kotlin gives to the single parameter inside the lambda, acting like a variable you can use directly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'it' at step 2?
A2
B1
C3
D4
💡 Hint
Check the 'Lambda Input (it)' column at step 2 in the execution_table.
At which step does the output list first contain [2, 4]?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Output List State' column in the execution_table.
If the lambda had two parameters, could we still use 'it'?
AYes, 'it' always works
BNo, 'it' only works for one parameter
COnly if we rename 'it'
DOnly if we use a different keyword
💡 Hint
Refer to the second key_moment about multiple parameters.
Concept Snapshot
Kotlin lambdas with one parameter can use 'it' implicitly.
No need to name the parameter explicitly.
Use 'it' inside the lambda body to refer to the parameter.
Example: list.map { it * 2 } doubles each element.
If more than one parameter, 'it' cannot be used.
Full Transcript
This example shows how Kotlin lets you use 'it' as a shortcut for the single parameter in a lambda. The code doubles each number in a list by multiplying 'it' by 2. The execution table traces each step: the input value 'it', the operation, the result, and how the output list grows. Beginners often wonder why 'it' works without declaration; it's because Kotlin automatically names the single parameter 'it'. If there were more parameters, you would have to name them explicitly. This visual helps you see how 'it' changes with each element and how the final list is built.