Post

Arrow Functions

The Arrow function is represented by => symbol. 

Arrow Functions

If you want to declare a function in one line; In Dart we have a fat arrow function that can enable you. The function is represented by => symbol. 

Syntax 

Below is the syntax for the arrow function

1
returnType functionName(parameters) => expression;

Knowledge Panel 

Note: The arrow function is used to make your code short. => expr syntax is a shorthand for { return expr; }.

Example 1: WITHOUT Arrow Function

This program finds simple interest without using the arrow function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  void main() {
    // Principal amount, rate of interest, and time period
    double principal = 1000.0;
    double rate = 5.0;
    double time = 3.0;

    // Function to calculate simple interest
    double calculateSimpleInterest(double p, double r, double t) {
      return (p * r * t) / 100;
    }

    // Calling the function and storing the result
    double interest = calculateSimpleInterest(principal, rate, time);

    // Printing the result
    print("The simple interest is: \$${interest}");
  }

Example 2: WITH Arrow Function

This program finds simple interest using the arrow function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void main() {
    // Principal amount, rate of interest, and time period
    double principal = 1000.0;
    double rate = 5.0;
    double time = 3.0;

    // Arrow function to calculate simple interest
    double calculateSimpleInterest = (double p, double r, double t) => (p * r * t) / 100;

    // Calling the function and storing the result
    double interest = calculateSimpleInterest(principal, rate, time);

    // Printing the result
    print("The simple interest is: \$${interest}");
  }
This post is licensed under CC BY 4.0 by the author.