Difference between revisions of "W1201 Scope"

From Coder Merlin
Line 33: Line 33:
</syntaxhighlight>
</syntaxhighlight>


For this project you'll repeatedly edit the same file.  It's probably easiest to test this file ''within emacs'' by typing:
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.
'''M-&''' (async-shell-command)  and observing the results.  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

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Scope

Prerequisites[edit]

Research[edit]

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]

Typical Memory Layout

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.

Hint.pngHelpful 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 ALT-SHIFT-7. The command to execute each time is: swift main.swift


1

let x = 10
print(x)

What is output? (Ignore whitespace)

10
23
Compile or runtime error
21

2

let x = 10
print(x)
print(y)

What is output? (Ignore whitespace)

Compile or runtime error
10 10
21 23
10 21

3

let x = 10
print(x)
let x = 21
print(x)

What is output? (Ignore whitespace)

10 10
10 21
Compile or runtime error
21 23

4

let x = 10
print(x)
var x = 21
print(x)

What is output? (Ignore whitespace)

10 21
21 23
Compile or runtime error
10 10

5

let x = 10
print(x)
do {
    let x = 21
    print(x)
} //

10 10
10 21
21 21
Compile or runtime error

6

let x = 10
print(x)
do {
    let x = 21
    print(x)
} //
print(x)

Compile or runtime error
10 21 21
10 10 10
10 21 10

7

let x = 10
print(x)
repeat {
    let x = 21
    print(x)
} while x == 21
print(x)

10 21 21
Infinite loop
10 21 10
Compile or runtime error

8

let x = 10
print(x)
while x == 21 {
    let x = 21
    print(x)
} //
print(x)

Infinite loop
10 21 10
10 10
Compile or runtime error

9

let x = 10
print(x)
for x in 11 ... 11 {
    print(x)
    let x = x + 10
    print(x)
} //
print(x)

10 10 20 10
Compile or runtime error
10 11 21 10
Infinite loop

10

let x = 10
print(x)
for x in 11 ... 11 {
    print(x)
    x = x + 10
    print(x)
} //
print(x)

Compile or runtime error
Infinite loop
10 11 21 10
10 10 20 10