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.
val sum: (Int, Int) -> Int = { a, b -> a + b } val result = sum(3, 4) println(result)
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Declare lambda 'sum' with parameters a, b | Lambda created | sum holds lambda (a, b) -> a + b |
| 2 | Call sum(3, 4) | Invoke lambda with a=3, b=4 | Execute body: 3 + 4 |
| 3 | Calculate 3 + 4 | Addition | 7 |
| 4 | Assign result | result = 7 | result holds 7 |
| 5 | Print result | println(7) | Output: 7 |
| Variable | Start | After Step 1 | After Step 2 | After Step 4 | Final |
|---|---|---|---|---|---|
| sum | undefined | lambda (a, b) -> a + b | lambda (a, b) -> a + b | lambda (a, b) -> a + b | lambda (a, b) -> a + b |
| result | undefined | undefined | undefined | 7 | 7 |
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