0Pricing
Dart Academy · Lesson

Functions as First-Class Values

Pass and return functions like data.

Functions as First-Class Values is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Functions Are Values

In Dart, a function is a first-class value, just like a number or string. You can store it, pass it, and return it. 🎁

Storing a Function

Assign a function to a variable by using its name without parentheses. The variable now points to the function itself.

int triple(int x) => x * 3;
var f = triple;
f(4); // 12

The Function Type

Every function has a type describing its inputs and output. You can name it with the Function keyword for clarity.

int Function(int) op = triple;

Passing a Function In

A function can be a parameter of another function. The receiver calls it later, customizing behavior without rewriting code.

int apply(int x, int Function(int) fn) {
  return fn(x);
}

Higher-Order Functions

A function that takes or returns another function is a higher-order function. They power flexible, reusable building blocks.

apply(5, triple); // 15

Callbacks in Action

Passing a function so it runs later is called a callback. Collection methods like forEach rely on this pattern constantly.

[1, 2, 3].forEach(print);

Returning a Function

A function can build and hand back another function. The returned function can even remember values from where it was made.

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

Using the Returned Function

Call the maker, store its result, then invoke that result. Each closure keeps its own captured value alive.

var add5 = adder(5);
add5(10); // 15

Functions in Collections

Because functions are values, you can store many in a list or map and pick one to run at runtime.

var ops = [triple, add5];
ops[0](2); // 6

Mapping With Functions

Methods like map accept a function and apply it to each item. This turns a transformation into a reusable value you pass around.

[1, 2, 3].map(triple).toList(); // [3, 6, 9]

Why It Matters

First-class functions let you customize behavior by passing logic, not flags. This unlocks clean composition across all of Dart and Flutter. ✨

Quick Check

Let's see if first-class functions clicked.

Recap

You can now store, pass, and return functions, and build higher-order helpers. This is the foundation of functional style in Dart. 🎉

Frequently asked questions

Is the “Functions as First-Class Values” lesson free?

Yes — the full text of “Functions as First-Class Values” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Functions as First-Class Values”?

Pass and return functions like data. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Functions as First-Class Values” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Functions and Return Types
  2. Positional vs Named Parameters
  3. Default Values and required
  4. Functions as First-Class Values
← Back to Dart Academy