🧮 Functions

First-class Functions and Functional Programming in Dart

Basic Functions

Function with positional parameters:

file_type_dartlang functions.dart
  // Basic Function
  String takeFive(int number) {
    return '$number minus five equals ${number - 5}';
  }

Function with named parameters:

file_type_dartlang functions.dart
  // Named parameters
  namedParams({required int a, int b = 5}) {
    return a - b;
  }

  namedParams(a: 23, b: 10);

Arrow Functions

Arrow functions are useful when passing functions as parameters to other functions.

file_type_dartlang functions.dart
  // Arrow Function
  String takeFive(int number) => '$number minus five equals ${number - 5}';

Callback Functions

Many APIs in Dart use callback functions, often to handle events or gestures in Flutter.

file_type_dartlang functions.dart
  // First-class functions
  callIt(Function callback) {
    var result = callback();

    return 'Result: $result';
  }

Questions? Let's chat

Open Discord