Download presentation
Presentation is loading. Please wait.
Published byBrendan Evans Modified over 8 years ago
1
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1
2
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
3
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
4
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
5
void sub1() { int a, b;... 1 } /* end of sub1 */ void sub2() { int b, c;.... 2 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
7
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
8
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
9
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)
10
Named constants in java
11
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
12
int y; cin >> y; const int x = 2 * y; x++; cout << x; Syntax Error OK: Value Dynamically bound C++ example
13
const int x; X=5; cout << x; Error, need initialization C++ example
14
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
15
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
16
Copyright © 2012 Addison-Wesley. All rights reserved. 1-16 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.