Skip to content
Justice Tech School

Closures and Function Values

Functions are objects. They can be stored in variables and passed around. A closure captures variables from its surroundings.

Example
Function makeCounter() { var c = 0; return () => ++c; }
void main() {
  var next = makeCounter();
  print(next());
  print(next());
  var double = (int x) => x * 2;
  print(double(5));
}

Exercise

Function type.

 f = () => 1;

Quiz Test

What does a closure capture?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.