Lambda Expr
Lambda Expr
Lambda Expression
• an unnamed block of code (or an unnamed function) with a list of formal
parameters and a body. Sometimes called a lambda.
• The body of a lambda expression can be a block statement or an expression.
• describes an anonymous function.
• An arrow (->) is used to separate the list of parameters and the body.
Syntax for Lambda Expressions
() -> { } // Takes no parameters and returns void
// Takes an int parameter and returns the parameter value incremented by 1
(int x) -> x + 1
// Takes two int parameters and returns their sum
(int x, int y) -> x + y
// Takes two int parameters and returns the maximum of the two
(int x, int y) -> { int max = x > y ? x : y; return max; }
1
9/14/2022
2
9/14/2022