Dart – 7 – Functions

Functions in Dart Programming

Functions are a fundamental concept in Dart that allow you to encapsulate a set of instructions into a reusable block of code. They promote code organization, reusability, and maintainability. In this discussion, we will explore the concept of functions in Dart, how to declare and use them, and their significance in building modular and efficient code.

Declaring Functions

In Dart, you declare a function using the function name, a list of parameters, and a return type. The return type specifies the type of value the function will return, and if a function doesn’t return any value, you can use void as the return type.


returnType functionName(parameters) {
    // Function body
}
    

Here’s a simple example of a function that calculates the sum of two numbers:


int add(int a, int b) {
    return a + b;
}
    

In this example, the function add takes two integer parameters a and b and returns their sum as an integer.

Function Parameters

Functions in Dart can have zero or more parameters. Parameters allow you to pass values to a function, which can then be used within the function’s body. You can also specify named parameters, which provide more flexibility and readability in function calls.


returnType functionName({parameter1, parameter2, ...}) {
    // Function body
}
    

Here’s an example of a function with named parameters:


String greet({String name, String greeting}) {
    return '$greeting, $name!';
}

String message = greet(name: 'Alice', greeting: 'Hello');
    

In this example, the function greet accepts named parameters name and greeting

Function Return Values

Functions in Dart can return values using the return statement. The return type specified in the function’s declaration should match the type of value returned.


returnType functionName(parameters) {
    // Function body
    return value;
}
    

Here’s an example of a function that calculates the area of a rectangle and returns a double value:


double calculateArea(double length, double width) {
    return length * width;
}

double area = calculateArea(5.0, 3.0);
    

The function calculateArea returns the area as a double value.

Function Expressions

Dart provides a concise way to write small, single-expression functions using function expressions. These are particularly useful for simple functions with a single return statement.


returnType functionName(parameters) => expression;
    

Here’s an example of a function expression that doubles a given number:


int doubleValue(int number) => number * 2;
    

The function expression takes a single parameter number and returns the doubled value.

Function as First-Class Citizens

In Dart, functions are considered first-class citizens, which means they can be assigned to variables, passed as arguments to other functions, and returned from other functions. This feature allows for more flexible and functional programming in Dart.


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

int subtract(int a, int b) => a - b;

int calculate(int x, int y, Function operation) {
    return operation(x, y);
}

int result1 = calculate(10, 5, add);
int result2 = calculate(10, 5, subtract);
    

In this example, the calculate function takes two numbers and a function as its parameters, allowing it to perform addition or subtraction based on the provided operation function.

Conclusion

Functions are a cornerstone of Dart programming, enabling developers to organize code, make it reusable, and build modular and efficient applications. Understanding how to declare, use, and leverage the power of functions is essential for writing clean, maintainable, and flexible Dart code.