Difference between revisions of "W1201 Scope"
Line 33: | Line 33: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
For each question, be sure to ''understand'' the behavior before proceeding to the next question. It will be helpful to '''record''' your answers and reasoning for later review. | |||
{{Hint|The '''async-shell-command''' can be executed with {{VerySpecialKey|ALT|SHIFT|7}}. The command to execute each time is: ''swift main.swift''}} | {{Hint|For this project you'll repeatedly edit the same file. It's probably easiest to test this file ''within emacs'' by using the '''async-shell-command''' and observing the results. The '''async-shell-command''' can be executed with {{VerySpecialKey|ALT|SHIFT|7}}. The command to execute each time is: ''swift main.swift''}} | ||
Revision as of 21:03, 2 December 2019
Prerequisites[edit]
Research[edit]
- Read Scope (Wikipedia)
- Read Computer Science I Textbook (by Bourke) Chapter 2.2.4. Scoping
Introduction[edit]
The lifetime of a variable is the time period in which the variable has memory allocated to it and may legitimately be referenced.
The scope of a declaration is that part of the code in which the declaration is in effect.
The visibility of a declaration refers to that part of the code in which the declaration is accessible (not hidden) by an identical declaration in another scope.
To best understand these terms, we'll look at several examples below. Beforehand, however, let's revisit our discussion about L-Values.
Not All L-Values are Created Equal[edit]
Review our previous discussion about W1038 L-Values and R-Values. It's clear that the defining difference is that L-Values persist beyond a single expression, and are addressable somewhere in main memory. The big question is, where?
Experiment[edit]
Create a directory within your "project" directory.
cd ~/projects
mkdir project-1201
cd project-1201
Edit a new file named "main.swift"
emacs main.swift
For each question, be sure to understand the behavior before proceeding to the next question. It will be helpful to record your answers and reasoning for later review.