Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1
x = 0 def outer(): x = 1 def inner(): x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 1 # global: 0
x = 0 def outer(): x = 1 def inner(): nonlocal x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 2 # global: 0
x = 0 def outer(): x = 1 def inner(): global x x = 2 print("inner:", x) inner() print("outer:", x) outer() print("global:", x) # inner: 2 # outer: 1 # global: 2
void sub1() { int a, b;... 1 } /* end of sub1 */ void sub2() { int b, c; sub1(); } /* end of sub2 */ void main() { int c, d;... 3 sub2(); } /* end of main */ Point Referencing Environment 1) a and b of sub1, c of sub2, d of main, ( c of main and b of sub2 are hidden) 2) b and c of sub2, d of main, ( c of main is hidden) 3) c and d of main Point Referencing Environment 1) a and b of sub1, c of sub2, d of main, ( c of main and b of sub2 are hidden) 2) b and c of sub2, d of main, ( c of main is hidden) 3) c and d of main Assume dynamic scope
Copyright © 2012 Addison-Wesley. All rights reserved. 1-7 Chapter 5 Topics IntroductionNamesVariables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants
Copyright © 2012 Addison-Wesley. All rights reserved. 1-8 Named Constants A named constant is a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability The binding of values to named constants can be either static (called manifest constants) or dynamic
Copyright © 2012 Addison-Wesley. All rights reserved. 1-9 Named Constants –Ada, C++, and Java: expressions of any kind, dynamically bound binding of values Dynamic bindingStatic binding Value bound at run timeValue bound at compile time Ada, C++, and Java, C#(readonly ) C#(const)
Named constants in java
100 is written only one time and can’t be changed Named constants in java Final variables must be intlized in the same line of declaration
int y; cin >> y; const int x = 2 * y; x++; cout << x; Syntax Error OK: Value Dynamically bound C++ example
const int x; X=5; cout << x; Error, need initialization C++ example
In C# language const int x; x = 3; Error int a=4, b=5; const int y=a+b; Error const int y=5 OK Must be initialized when declared
class Program { public readonly int x =5; public readonly int y; Program() { int a = int.Parse(Console.ReadLine()); y = a + 1; y++; } public void f() { int a = int.Parse(Console.ReadLine()); y = a + 1; y++ } static void Main(string[] args) { } ok error OK C# readonly variables can be changed only inside constructor error
Copyright © 2012 Addison-Wesley. All rights reserved Summary Case sensitivity and the relationship of names to special words represent design issues of names Variables are characterized by the sextuples: name, address, value, type, lifetime, scope Binding is the association of attributes with program entities Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic Strong typing means detecting all type errors