Organizing code into functions

Dart:
Organizing code into functions

How to:

Basic Function

In Dart, you define a function using the void keyword if it doesn’t return a value, or specify the type of value it returns otherwise. Here’s a simple function that prints a greeting message:

void greet(String name) {
  print('Hello, $name!');
}

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

Returning a Value

Functions can return values. The following example takes two integers as input and returns their sum:

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

void main() {
  var sum = add(5, 3);
  print(sum);  // Output: 8
}

Anonymous Functions

Dart supports anonymous functions (also known as lambda expressions or closures), which can be handy for short, on-the-fly functionalities. Here’s how to use an anonymous function with a list’s forEach method:

void main() {
  var fruits = ['apple', 'banana', 'cherry'];
  fruits.forEach((item) {
    print(item);
  });
  // Output:
  // apple
  // banana
  // cherry
}

Arrow Syntax for Single-Expression Functions

For functions that only contain a single expression, Dart offers a concise syntax using the “arrow” notation (=>). This is especially useful for short functions or passing functions as arguments:

int square(int num) => num * num;

void main() {
  print(square(4));  // Output: 16
}

Using Third-Party Libraries

For more complex or specialized functionalities, Dart programmers often rely on third-party libraries. Consider the http library for making HTTP requests. First, add http to your pubspec.yaml file under dependencies:

dependencies:
  http: ^0.13.3

Then, you can use it to fetch data from the web:

import 'package:http/http.dart' as http;

Future<void> fetchUserData() async {
  var response = await http.get(Uri.parse('https://api.example.com/users/1'));
  print(response.body);
}

void main() {
  fetchUserData();
  // Expected output: JSON data of the user. Actual output will depend on the API's response.
}

Remember, when organizing your Dart code into functions, think about reusability, clarity, and the single responsibility principle. This not only makes your code cleaner but also easier for others (and future you) to understand and maintain.