Intro To Functions In Dart – Functions Dart Programming Guide

Haris Bin Nasir Avatar

·

·

Functions in Dart are blocks of code that perform specific tasks. They help in organizing code into reusable components, making your program modular and easier to maintain. Dart functions can be named or anonymous and can accept parameters and return values. Understanding how to define and use functions is essential for efficient programming in Dart.

For further information on detailed topics of Dart, click here.

Defining a Function

In Dart, you define a function using the following syntax:

returnType functionName(parameters) {
  // Function body
  return value;
}
  • returnType: The type of value the function will return. If the function doesn’t return anything, use 'void'.
  • functionName: The name of the function.
  • parameters: Optional. The values passed to the function.

Example 1:

void sayHello() {
  print('Hello, Dart!');
}

void main() {
  sayHello();  // Output: Hello, Dart!
}

Example 2:

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

void main() {
  int sum = add(3, 4);
  print(sum);  // Output: 7
}

Non-Parameterized Functions

A non-parameterized function in Dart does not accept any arguments. It performs its task without needing external input.

Example 1:

void printHello() {
  print('Hello, World!');
}

void main() {
  printHello();  // Output: Hello, World!
}

Example 2:

void SquareOfTwo() {
  int result = 2 * 2;
  print('The square of 2 is $result');
}

void main() {
  SquareOfTwo();  // Output: The square of 2 is 4
}

Optional Parameters

Dart supports optional parameters, which can be either positional or named.

  • Positional Optional Parameters: These parameters are enclosed in square brackets and are optional when calling the function.
void greet(String name, [String message = "Hello"]) {
  print('$message, $name!');
}

void main() {
  greet('Alice');  // Output: Hello, Alice!
  greet('Alice', 'Hi');  // Output: Hi, Alice!
}
  • Named Optional Parameters: These parameters are enclosed in curly braces and allow you to specify values by name.
void greet({String? name, String message = "Hello"}) {
  print('$message, $name!');
}

void main() {
  greet(name: 'Alice');  // Output: Hello, Alice!
}

Arrow Functions

For concise functions, Dart offers arrow syntax (=>), which is a shorthand for functions that consist of a single expression.

Example 1:

int multiply(int a, int b) => a * b;

void main() {
  print(multiply(3, 4));  // Output: 12
}

Example 2:

List<String> names = ['Alice', 'Bob', 'Charlie'];
names.forEach((name) {
  print('Hello, $name');
});
// Output:
// Hello, Alice
// Hello, Bob
// Hello, Charlie

Anonymous Functions

Anonymous functions (also known as lambda functions or closures) are functions without a name. These functions are often used as arguments in other functions or assigned to variables.An anonymous function is simply a function that doesn’t have a name. You can create it and use it on the spot without needing to define it separately.

Example:

var numbers = [1, 2, 3, 4];
numbers.forEach((number) {
  print(number);
});

First-Class Functions

In Dart, functions are first-class citizens, meaning they can be assigned to variables, passed as parameters, and returned from other functions.

Example 1:

void printMessage(String message) {
  print(message);
}

void executeFunction(void Function(String) func) {
  func("Hello, Dart!");
}

void main() {
  executeFunction(printMessage);  // Output: Hello, Dart!
}

Example 2:

void shout(String message) {
  print(message.toUpperCase());
}

void execute(void Function(String) func, String value) {
  func(value);
}

void main() {
  execute(shout, 'hello');  // Output: HELLO
}

Returning Functions

You can also return functions from other functions, which is useful for creating function factories.

Example 1:

Function multiplier(int factor) {
  return (int x) => x * factor;
}

void main() {
  var multiplyBy2 = multiplier(2);
  print(multiplyBy2(3));  // Output: 6
}

Example 2:

Function addBy(int n) {
  return (int x) => x + n;
}

void main() {
  var addBy5 = addBy(5);
  print(addBy5(10));  // Output: 15
}

Conclusion

Dart programming relies on functions to help you organize your code into reusable, modular components. By mastering Dart’s function syntax, you can efficiently write clean code, including the use of optional parameters, anonymous functions, and first-class functions.

Happy Coding…!!!

Leave a Reply

Your email address will not be published. Required fields are marked *