LearnViz CompSci
Home / Learn / Programming Fundamentals / Closures

Intermediate Topic 34 of 54 Programming Fundamentals

Closures

Functions that capture variables from their defining scope.

How it works

A closure is a function bundled together with the variables of the scope in which it was defined, and those variables stay alive after the outer function returns. This lets the function keep private state without a class and allows function factories that produce customized behavior. The runtime keeps captured variables on the heap rather than the stack.

Step-by-step walkthrough

  1. make_counter() creates a local variable count.
  2. It defines an inner function that uses count.
  3. It returns the inner function, and count stays alive because it is captured.
  4. Each call to the inner function updates the same captured count.
  5. Each call to make_counter() gets its own separate count.

Where it is used

Counters, memoization wrappers, event handlers that remember context, callbacks that need configuration and module patterns for private data all use closures. They are fundamental to JavaScript and functional programming.

Common pitfall

Capturing loop variables by reference can make all callbacks see the last value in some languages. Closures can also keep large objects alive and cause memory leaks. Remember that captured variables are shared, not copied, so later changes are visible to every closure using them.

Where it fits in the course

Closures is topic 34 of 54 in Programming Fundamentals, rated Intermediate. It follows Lambda Expressions. The next topic is Iterators.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Closures?
Functions that capture variables from their defining scope. It belongs to Programming Fundamentals and is an intermediate-level topic.
Where do people use Closures?
Counters, memoization wrappers, event handlers that remember context, callbacks that need configuration and module patterns for private data all use closures.
What is the most common mistake with Closures?
Capturing loop variables by reference can make all callbacks see the last value in some languages.
What should I learn before this topic?
Start with Lambda Expressions, the topic just before it in Programming Fundamentals.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Programming Fundamentals quiz.
Is Closures hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Closures?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Closures?
Lambda Expressions, Iterators, Higher-Order Functions are closely connected. The full list is in the related topics section of this page.
What should I study after Closures?
Continue with Iterators, the next topic in Programming Fundamentals.

Related topics