0
0
Kotlinprogramming~10 mins

Lambda syntax and declaration in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lambda syntax and declaration
Start
Declare Lambda
Assign Lambda to Variable
Call Lambda
Execute Lambda Body
Return Result
End
This flow shows how a lambda is declared, assigned to a variable, called, and executed to produce a result.
Execution Sample
Kotlin
val sum: (Int, Int) -> Int = { a, b -> a + b }
val result = sum(3, 4)
println(result)
This code declares a lambda that adds two integers, calls it with 3 and 4, and prints the result.
Execution Table
StepActionEvaluationResult
1Declare lambda 'sum' with parameters a, bLambda createdsum holds lambda (a, b) -> a + b
2Call sum(3, 4)Invoke lambda with a=3, b=4Execute body: 3 + 4
3Calculate 3 + 4Addition7
4Assign resultresult = 7result holds 7
5Print resultprintln(7)Output: 7
💡 Lambda executed and result printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
sumundefinedlambda (a, b) -> a + blambda (a, b) -> a + blambda (a, b) -> a + blambda (a, b) -> a + b
resultundefinedundefinedundefined77
Key Moments - 3 Insights
Why do we write the lambda parameters before the arrow '->' inside the braces?
The parameters before '->' define inputs to the lambda. This is shown in Step 1 where 'a, b' are declared before '->' to receive values when called.
How does the lambda get called and executed?
In Step 2, calling sum(3, 4) passes 3 and 4 to the lambda parameters, then Step 3 executes the body 'a + b' with these values.
What type does the variable 'sum' have?
In Step 1, 'sum' is declared with type '(Int, Int) -> Int', meaning it takes two Ints and returns an Int, matching the lambda signature.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after Step 4?
A7
B3
Cundefined
Dsum lambda
💡 Hint
Check the 'result' variable in variable_tracker after Step 4.
At which step is the lambda body 'a + b' actually executed?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Evaluation' column in execution_table where addition happens.
If we change the lambda to { x, y -> x * y }, what changes in the execution table?
AStep 5 output changes to 7
BStep 3 result changes to 12
CStep 2 changes to call sum(3, 4)
DNo changes
💡 Hint
Multiplying 3 and 4 gives 12 instead of 7, check Step 3 result.
Concept Snapshot
Lambda syntax in Kotlin:
val name: (Type1, Type2) -> ReturnType = { param1, param2 -> expression }
- Parameters before '->' inside braces
- Expression after '->' is the body
- Call lambda like a function: name(arg1, arg2)
- Lambda returns the last expression value
- Useful for short, inline functions
Full Transcript
This visual trace shows how to declare and use a lambda in Kotlin. First, a lambda is declared with parameters and a body expression. It is assigned to a variable with a function type. Then, calling the lambda passes arguments to parameters, executes the body, and returns the result. The variable 'result' stores this output, which is printed. Key points include the syntax of parameters before the arrow, calling the lambda like a function, and the lambda returning the last expression. The execution table and variable tracker help visualize each step clearly.