Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
69 views

Scopes and Data Types: CMSC 124

The document discusses scope, static scope, dynamic scope, referencing environment, named constants, and data types in programming languages. It defines scope as the range of statements in which a variable is visible. Static scope is determined prior to execution based on the program text, while dynamic scope uses the calling sequence of subprograms. Referencing environment is the collection of all variables visible in a statement. Named constants improve readability by binding a variable to an unchangeable value. Data types define collections of values and operations on those values to aid type checking and readability.

Uploaded by

Roel Dagdag
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Scopes and Data Types: CMSC 124

The document discusses scope, static scope, dynamic scope, referencing environment, named constants, and data types in programming languages. It defines scope as the range of statements in which a variable is visible. Static scope is determined prior to execution based on the program text, while dynamic scope uses the calling sequence of subprograms. Referencing environment is the collection of all variables visible in a statement. Named constants improve readability by binding a variable to an unchangeable value. Data types define collections of values and operations on those values to aid type checking and readability.

Uploaded by

Roel Dagdag
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Scopes and Data types

CMSC 124
Scope

• Range of statements in which the variable is visible


• Visible – if it can be referenced in that statement
• The scope rules of a language determine how a
particular occurrence of a name is associated with a
variable
Scope

• A variable is said to be local to a program unit or


block if it is declared there
• All other variables that are visible to the block but
are not declared there are nonlocal variables
Static Scope

• Scope can be determined prior to execution


• ALGOL 60 introduced this static scoping
• Some languages allows nested subprograms, which
creates nested static scopes
– Ada, Javascript, Common LISP, Scheme, Fortran 2003+
and Python.
– C-based languages do not.
Static Scope
Based on program text
– To connect a name reference to a variable, you (or the
compiler) must find the declaration
– Search process: search declarations, first locally, then in
increasingly larger enclosing scopes, until one is found for
the given name
– First level up the subprogram where declaration is found is
called static parent
– The static parent of the static parent and so forth up to
largest enclosing subprogram –static ancestors
Static Scope
• Javascript
function big() {
function sub1(){ • For static scoping x in sub2()
var x=7; references x declared in big()
sub2();
} • Some variables can be hidden
function sub2(){ from some other code segments
var y =x;
} like x in big() is hidden from the
var x = 3; x used in sub1()
sub1();
}
Static Scope

• Blocks – section of code having its own local variables


whose scope is minimized (introduced by ALGOL 60)
• Ex. In C –based languages, allows any compound
statement (surrounded by braces) to have declarations
and therefore define new scope
if (list[i] < list[j]) {
int temp;
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
Static Scope
• Declaration orders:
• Allow variables to appear anywhere in a program – C99,
C++, Java, Javascript and C#
• Some need to define all variables at the beginning – C89
• Scope can be not associated with subprograms – C99,
C++ and Java – variable scope is when they are declared
until the end of the block they are declared
• In C#, scope of a variable is the whole block regardless
where in the block it is declared
Static Scope

• Global scope
• Global variables are variables declared outside the
functions
– In C, declaration outside the function uses extern ie.,
extern int sum
– In PHP, globals can be accessed thru the $GLOBAL
array or no local variable must have the same name
Static Scope
• Global scope in PHP
$day = "Monday";
Interpretation of this code produces the
$month = "January"; following:
function calendar() { local day is Tuesday
$day = "Tuesday"; global day is Monday
global $month; global month is January
print "local day is $day <br />";
$gday = $GLOBALS['day'];
print "global day is $gday <br \>";
print "global month is $month <br />";
}
calendar();
Dynamic Scope

• Use in languages such as APL and LISP


• Based on the calling sequence of subprograms, not
on the physical location of their declarations
• References to variables are connected to declarations
by searching back through the chain of subprogram
calls that forced execution to this point
Dynamic Scope
• Javascript
function big() {
function sub1(){ • Reference of x in sub2 is the x
var x=7;
sub2(); declared in sub1
} • In Static Scope, x in sub2 is the x
function sub2(){
var y =x; declared in big()
}
var x = 3;
sub1();
}
Dynamic Scope

• Disadvantage: Difficult to read because the calling


sequence must be known
– Access to non local variables takes far longer then
when using static scope
• Advantage: No need to pass parameters to
subprograms because the variables are implicitly
visible to subprograms
Scope and Lifetime

• Scope is from its declaration to the end of the


method while lifetime is the period of time beginning
when the method is entered and ending when
execution of that method terminates.
• These are usually related, but not in all cases
• It is important to remember that these are two
different attributes and should be treated distinctly
Scope and Lifetime

• Consider the C++ functions


• Scope of sum is
void printheader() {
contained in within
...
} /* end of printheader */
compute() function but is
void compute() { not accessible in
int sum; printheader()
... • Lifetime of sum extends
printheader(); over the time until
} /* end of compute */ printheader() executes
Referencing Environment

• The referencing environment of a statement is the


collection of all variables that are visible in the
statement.
• For static-scoped languages, RE is the variables
declared in its local scope plus the collection of all
variables of its ancestor scopes that are visible
g = 3; # A global • In the Python skeleton
def sub1(): program: (static scope)
a = 5; # Creates a local
b = 7; # Creates another local Referencing Environment
... 1
def sub2(): 1 local a and b (of sub1), global
g for reference, but not
global g; # Global g is now assignable here for assignment
c= 9; # Creates a new local 2 local c (of sub2), global g for
... 2 both reference and for
def sub3(): assignment
3 nonlocal c (of sub2), local g
nonlocal c: # Makes nonlocal c visible here (of sub3)
g = 11; # Creates a new local
... 3
Referencing Environment

• The referencing environment of a statement in a


dynamically scoped language is the locally declared
variables, plus the variables of all other subprograms
that are currently active.
• A subprogram is active if its execution has begun but
has not yet terminated.
void sub1() {
int a, b;
... 1 Dynamic Scope
} /* end of sub1 */
void sub2() { Referencing Environment
int b, c; 1 a and b of sub1, c of sub2, d of
.. . . 2 main, (c of main and b of
sub1();
sub2 are hidden)
} /* end of sub2 */
void main() { 2 b and c of sub2, d of main, (c
int c, d; of main is hidden)
... 3 3 c and d of main
sub2();
} /* end of main */
Named Constants

• A variable is bound to a value only at the time it is bound to


storage
• Once it is assigned a value, it cannot be changed for the entire
duration of the program’s execution
• Improves readability
• Ex. In Java: For C++:
void example() { const int result = 2 * width + 1;
final int len = 100;
int[] intList = new int[len];
String[] strList = new String[len];
Data Type

• A data type defines a collection of data values and a


set of predefined operations on those values.
• How well the data types match those used in the real
world determine the ease with which they perform
this task
Data Type

• A good language should provide a set of primitive


data types as well as user-defined types
– Aid readability
– Allows type checking to be performed
Data Type

• Data type design allows the user to create a unique


type for each unique class of variables use in the
problem
• A descriptor (implementation of a symbol table),
stores the attributes of a variable
Primitive Data Types

• Data types that are not defined in terms of other


data types
• Example:
– Numeric types
– Character string types
– User-defined ordinal types
Numeric types

• Basic type supported by all languages, since the computer is


first and foremost, a number crunching machine
• Example:
– Integers
– Floating point
– Decimal
– Boolean types
– Character types
Integers

• The most common primitive numeric data type


• Can come in different sizes, determining the range that it
can represent
• Uses several formats to represent numbers
– Unsigned
– Signed (sign magnitude, one’s complement, two’s
complement, etc)
• Java supports (signed integers) byte, short, int, and long
Floating Point

• Used to model real numbers


• Since the computer is a finite machine, it cannot
possibly model the entire real number space which is
infinitely long and infinitely dense
• As such, floating point data types store mere
approximations
Floating Point

• Real numbers are modeled by four characteristics


– Sign
– Mantissa
– Base
– Exponent
• represented as fractions and exponents, a form that is
borrowed from scientific notation
Decimal

• Also known as binary coded decimals


• Store a fixed number of decimal digits, with a decimal pt
at a fixed position in the value – business apps.
• Similar to the integer data type, except that its storage
method lends itself well to decimal manipulation
• Range of values is restricted and the storage method is
wasteful
Boolean Types

• Can represent only two values, true or false


• Used for boolean arithmetic, switches on flag variables
• Uses only a single bit, but for most computers, the
minimum addressable unit of memory is larger than a bit
• Therefore, boolean types are represented using this
minimum unit, usually a byte (8 bits)
Character Types

• Stores character data as numeric data using a


character map such as 8-bit code ASCII
• Because of globalization of business and the need to
communicate with other computers around the
world, a 16 bit character set named Unicode has
been developed as an alternative
– Ex. Java
String Operations

• C and C++ use char arrays to store character strings


• In Java, strings are supported by the String class
• Some languages allow accessing the individual characters in
the string
• Assignment
• Concatenation
• Comparison
• Pattern matching
– /[A-Za-z][A-Za-z\d]+/
String Length

• Static string length – Java, Python, C++


• Limited dynamic length string - C
• Dynamic length string – Javascript, Perl
User Defined Ordinal Types

• An ordinal type is one in which the range of possible


values can be easily associated with the set of
positive integers
• There are two user-defined ordinal types that have
been supported by programming languages:
enumeration and subrange.

You might also like