tezvyn:

What is a Dart closure? Write a function that returns a function.

AI-drafted, machine-checkedSource: dart.devintermediate

Tests lexical scoping: define a closure as a function plus its captured environment, show a makeAdder where the inner function uses a parent variable after the parent returns.

WHAT THIS TESTS: The interviewer wants to know if you understand that Dart functions are first-class objects and that lexical scoping means functions capture variables from their enclosing scope rather than only seeing the scope where they are invoked. This concept is foundational for building callbacks, managing state in Flutter, and using functional programming patterns effectively.

A GOOD ANSWER COVERS: First, define a closure precisely as a function object combined with the variables in its lexical scope that it references. Second, write a concrete example such as a function named makeAdder that takes an integer addBy and returns another function that takes a number and returns the sum of that number and addBy. Third, explain that after makeAdder returns and its stack frame is gone, the inner function still has access to addBy because Dart captures the variable by reference in the closure rather than copying its value at creation time. Fourth, mention that in Dart all functions have the type Function and can be assigned to variables or passed as arguments, which is why closures are a natural language feature.

COMMON WRONG ANSWERS: Do not say a closure is simply an anonymous function because named nested functions also create closures. Do not claim the parent function stays on the call stack after it returns; Dart captures the variable in the heap, not the stack frame. Do not say the captured variable is copied by value unless you explicitly clarify that Dart captures the variable binding, meaning mutations to a mutable captured object are visible across all closures that captured it. Avoid giving a global variable example instead of a true nested scope example because that misses the point of lexical capture.

LIKELY FOLLOW-UPS: The interviewer might ask how closures interact with loops, for example what happens if you create multiple closures inside a for loop all referencing the same loop variable. They might ask whether closures capture by value or by reference in Dart, or how closures affect memory management and garbage collection when they hold references to large objects. Another follow-up is asking you to compare closures to classes for state management or to refactor a StatefulWidget pattern into a function that returns a closure to manage local state.

ONE CONCRETE EXAMPLE: Write this in a code editor or on a whiteboard: Function makeAdder(int addBy) { return (int i) => addBy + i; } Then show usage: var addTwo = makeAdder(2); assert(addTwo(3) == 5); Explain that addTwo is a closure holding a reference to addBy with the value 2, and it works correctly even though makeAdder has already finished executing and its local scope has ended.

Read the original → dart.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.