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:
- Subprograms can be nested
- A subprogram can access nonlocal variables
- The subprogram can be called outside its defining scope
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:
- 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.
- 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:
- Define a nested subprogram
- Access a nonlocal variable from the enclosing scope
- Create and return a closure
- Show that the closure preserves its referencing environment
- Create multiple closures with different environments
- Demonstrate how captured variables behave when their values change
- Create closures inside a loop and analyze the loop variable behavior
- 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:
- Provide code
- Provide output
- Explain the behavior
- Explain how the language addresses the two design issues above
Part B - Evaluation (10 points)
Compare languages in terms of:
- Readability
- Writability
- Clarity of closure semantics
- Behavior of captured variables (value vs. reference-like)
- Loop variable behavior
Part C - Learning strategy (10 points)
Explain:
- Tools used
- Experiments performed
- Resources used
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:
- A single file for the Report, containing parts A, B, and C: ID_lastname_name_report.pdf (65 Points)
- A folder called, Codes, for Part D. The contents of the folder will be as follows:
- A single file for Dart code: ID_lastname_name.dart (5 points)
- A single file for Go code:ID_lastname_name.go (5 points)
- A single file for JavaScript code: ID_lastname_name.html (5 points)
- A single file for Lua code: ID_lastname_name.lua (5 points)
- 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:
- Late submissions will be accepted, with 20 points (out of 100) deduction for each extra day.
- You may use the tutorials available on the Internet as a reference,
but do not derive your example from the contents of the tutorials.
If you do so, your programs may be similar to others in the class,
that causes a disciplinary investigation.
- Collaboration on the homework is not allowed.