Functions First-class Functions and Functional Programming in Dart

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access

OR


*Enrollment provides full access to this course (and updates) for life.

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';
  }

Q&A Chat