CS 315
Homework Assignment 2


Assigned:  April 15, 2026
Due:  April 22, 2026, 23:55

Closures and Referencing Environments in Dart, Golang, JavaScript, Lua, and Python

A closure is a subprogram and the referencing environment where it is defined. The referencing environment consists of all variables that are visible (in-scope) at the time the closure is created.

Closures are only needed when:

In this homework, you will investigate how closures and referencing environments are supported in the following programming languages: Dart, Golang, JavaScript, Lua, and Python.

In addition, you will investigate the following design issues related to closures:

  1. Capture Semantics (Value vs. Reference-like behavior)
    Does the closure capture a snapshot of a variable, or does it reflect later changes to that variable? Illustrate how each language behaves.
  2. The Loop Variable Trap
    When closures are created inside a loop, do they all share the same loop variable, or does each iteration create a new binding? Demonstrate the behavior and how (if necessary) it can be avoided.

You can use online compilers/interpreters.

For each language, prepare a single source code file that demonstrates the following tasks:

  1. Define a nested subprogram
  2. Access a nonlocal variable from the enclosing scope
  3. Create and return a closure
  4. Show that the closure preserves its referencing environment
  5. Create multiple closures with different environments
  6. Demonstrate how captured variables behave when their values change
  7. Create closures inside a loop and analyze the loop variable behavior
  8. Show that closures continue to work even after the outer subprogram is no longer directly accessible

Your example programs must be your own. Avoid plagiarism.

Part A - Closures in each language (45 points)

For each language and each task:

Part B - Evaluation (10 points)

Compare languages in terms of:

Part C - Learning strategy (10 points)

Explain:

Part D - Sample Codes (35 points)

All code must compile and run correctly.

Submission:

A single zip or rar file should be submitted containing the following files with given names:

  1. A single file for the Report, containing parts A, B, and C: ID_lastname_name_report.pdf (65 Points)
  2. A folder called, Codes, for Part D. The contents of the folder will be as follows:
    1. A single file for Dart code: ID_lastname_name.dart (5 points)
    2. A single file for Go code:ID_lastname_name.go (5 points)
    3. A single file for JavaScript code: ID_lastname_name.html (5 points)
    4. A single file for Lua code: ID_lastname_name.lua (5 points)
    5. A single file for Python code: ID_lastname_name.py (5 points)

Please upload the zip or rar file you created to Moodle before the due date.

Important Notes: