0
0
Kotlinprogramming~10 mins

Passing lambdas to functions in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Passing lambdas to functions
Define lambda
Define function with lambda parameter
Call function, pass lambda
Inside function: invoke lambda
Use lambda result or side effect
Function ends
This flow shows how a lambda is defined, passed to a function, invoked inside it, and then the function completes.
Execution Sample
Kotlin
fun operateOnNumber(x: Int, op: (Int) -> Int): Int {
    return op(x)
}

val result = operateOnNumber(5) { it * 2 }
println(result)
This code defines a function that takes a number and a lambda, applies the lambda to the number, and prints the result.
Execution Table
StepActionEvaluationResult
1Call operateOnNumber with x=5 and lambda { it * 2 }Pass x=5 and lambdaFunction starts
2Inside operateOnNumber, invoke lambda with x=5lambda(5) = 5 * 210
3Return lambda result 10 from functionReturn 10result = 10
4Print resultprintln(10)Output: 10
💡 Function ends after returning the lambda result and printing it.
Variable Tracker
VariableStartAfter CallAfter Lambda InvocationFinal
xundefined555
op (lambda)undefined{ it * 2 }{ it * 2 }{ it * 2 }
resultundefinedundefinedundefined10
Key Moments - 3 Insights
Why do we write { it * 2 } outside the parentheses when calling the function?
In Kotlin, if the last parameter of a function is a lambda, you can pass it outside the parentheses. See execution_table step 1 where the lambda is passed this way.
What does 'it' mean inside the lambda?
'it' is the implicit name for the single parameter of the lambda. In step 2, 'it' is 5, the value passed to the lambda.
How does the function get the lambda result?
The function calls op(x) in step 2, which runs the lambda and returns the result 10, then returns it in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what is the value of 'it' inside the lambda?
A2
B10
C5
Dundefined
💡 Hint
Check the 'Evaluation' column in step 2 where lambda(5) = 5 * 2
At which step does the function return the lambda's result?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in step 3 about returning the lambda result
If we change the lambda to { it + 3 }, what will be printed?
A15
B8
C10
D5
💡 Hint
Refer to variable_tracker 'result' and how lambda transforms x=5
Concept Snapshot
Passing lambdas to functions in Kotlin:
- Define function with lambda parameter: fun f(x: Int, op: (Int) -> Int)
- Call function passing lambda outside parentheses if last param: f(5) { it * 2 }
- Inside function, invoke lambda: op(x)
- Lambda uses 'it' for single param
- Function returns lambda result
Full Transcript
This example shows how to pass a lambda to a Kotlin function. The function operateOnNumber takes an integer and a lambda that transforms an integer. When called with 5 and the lambda { it * 2 }, the function invokes the lambda with 5, getting 10. It returns 10, which is printed. The lambda parameter 'it' represents the input number. Kotlin allows passing the lambda outside parentheses if it is the last argument. This flow helps beginners see how lambdas are passed, invoked, and results returned.