Instructor: James Riely
x
in the initializer, x + 1
int main (void) {
int x = 10;
{
int x = x + 1;
printf ("x = %08x\n", x);
}
return 0;
}
$ gcc -o scope scope.c
$ gcc -Wall -o scope scope.c
scope.c: In function ‘main’:
scope.c:5:7: warning: unused variable ‘x’ [-Wunused-variable]
scope.c:7:9: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
$ ./scope
x = 00000001
class C {
public static void main (String[] args) {
int x = 1 + x;
System.out.printf ("x = %08x\n", x);
}
}
x.java:3: error: variable x might not have been initialized
int x = 1 + x;
^
1 error
scala> val x:Int = 1 + x
x: Int = 1
var x:Int = 10
def foo () =
x = 20
def bar () =
var x:Int = 30
foo ()
bar ()
println (x)
z
come from?
...
def g (x:Int) : Int =
var y:Int = x * 2
z * x * y // x and y are local; z is non-local
var x:Int = 10
def foo () : Unit =
x = 20
def bar () : Unit =
var x:Int = 30
foo ()
bar ()
println (x)
Bash (prints 10):
x=10
function foo() {
x=20
}
function bar() {
local x=30
foo
}
bar
echo $x
C functions (prints 20):
int x = 10;
void foo () {
x = 20;
}
void bar () {
int x = 30;
foo ();
}
int main () {
bar ();
printf ("x=%d\n", x);
}
C macros (prints 10):
int x = 10;
#define foo() { \
x = 20; \
}
#define bar() { \
int x = 30; \
foo (); \
}
int main () {
bar ();
printf ("x=%d\n", x);
}
Python (prints 20):
def main():
def foo ():
nonlocal x
x = 20
def bar ():
x = 30
foo ()
x = 10
bar ()
print (x)
main()
Python (prints 20):
def foo ():
global x
x = 20
def bar ():
x = 30
foo ()
x = 10
def main():
bar ()
print (x)
main()
Python global scope is not static
def useX():
print (x)
def defX():
global x
x = 1
>>> useX()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in useX
NameError: name 'x' is not defined
>>> defX()
>>> useX()
1
Emacs Lisp (prints "10"):
(let ((x 10))
(defun foo ()
(setq x 20))
(defun bar ()
(let ((x 30))
(foo)))
(bar)
(message (int-to-string x)))
Common Lisp (prints 20):
(let ((x 10))
(defun foo ()
(setq x 20))
(defun bar ()
(let ((x 30))
(foo)))
(bar)
(print x))
Scheme (prints 20):
(let ((x 10))
(define (foo)
(set! x 20))
(define (bar)
(let ((x 30))
(foo)))
(bar)
(display x)
(newline))
Perl (prints 10):
local $x = 10;
sub foo {
$x = 20;
}
sub bar {
local $x = 30;
foo ();
}
bar ();
print ($x);
Perl (prints 20):
my $x = 10;
sub foo {
$x = 20;
}
sub bar {
my $x = 30;
foo ();
}
bar ();
print ($x);