Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

How to use Lambda functions in Dart programming

Maria Elijah

Lambda functions are a short and simple way to express tiny functions. Lambda functions are also known as arrow functions. Like any other function, a Lambda function cannot execute a block of code.

return_type func_name(parameters)=> expression;

Note: You can only return one expression when using Lambda function syntax, which has to be a single-line expression.

Examples

How to use a Lambda function to add two numbers

// Using lambda function

int addNum(int a, int b) => a + b;

main() {
  print(addNum(15, 7));
}

Note: A return statement isn’t explicitly required.

How to use a Lambda function to display a message on the screen

void main() { 
   displayMsg(); 
} 

displayMsg()=>
print("This is a shot on Lambda functions in Dart programming"); 

How to use a Lambda function to concatenate strings

// Using lambda function

String showData(String a, String b) => a + b;

main() {
  print(showData("Educative ", "shot"));
}

Finally, if a function only returns one expression, you may use the Lambda function to represent it in only one line.

RELATED TAGS

dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring