Chapter 5 Variables Names Address Types Variables Assignment Binding Lifetime Scope Constants
Total Page:16
File Type:pdf, Size:1020Kb
Topics Imperative Paradigm Chapter 5 Variables Names Address Types Variables Assignment Binding Lifetime Scope Constants Chapter 5: Variables 2 Imperative Paradigm Von Neumann Architecture The most widely used and well-developed programming paradigm. The architecture of the von Neumann Emerged alongside the first computers and machine has a memory, which computer programs in the 1940s. contains both program instructions and Its elements directly mirror the architectural data values, and a processor, which characteristics of most modern computers provides operations for modifying the This chapter discusses the key programming contents of the memory. language features that support the imperative paradigm. Chapter 5: Variables 3 Chapter 5: Variables 4 Programming Language: Turing Von Neumann Architecture Complete Turing complete: contains integer variables, values, and operations, assignment statements and the control, constructs of statement sequencing, conditionals, and branching statements. n Other statement forms (while and for loops, case selections, procedure declarations and calls, etc) and data types (strings, floating point values, etc) are provided in modern languages only to enhance the ease of programming various complex applications. Chapter 5: Variables 5 Chapter 5: Variables 6 1 Imperative Programming Variables Language Turing complete A variable is an abstraction of a memory Also supports a number of additional cell or collection of cells. fundamental features: n Integer variables are very close to the n Data types for real numbers, characters, strings, Booleans and their operands. characteristics of the memory cells: n Control structures, for and while loops, case (switch) represented as an individual hardware statements. memory word. n Arrays and element assignment. n A 3-D array is less related to the organization n Record structures and element assignment. of the hardware memory: a software mapping n Input and output commands. is needed. n Pointers. n Procedure and functions. Chapter 5: Variables 7 Chapter 5: Variables 8 Variables: attributes Names A variable can be thought of as being Names have broader use than simple for completely specified by its 6 basic variables. attributes (6-tuple of attributes). Names or identifiers are used to denote 1. Name: identifier language entities or constructs. 2. Address: memory location(s) n In most languages, variables, procedures and 3. Value: particular value at a moment constants can have names assigned by the 4. Type: range of possible values programmer. 5. Lifetime: when the variable is accessible Not all variables have names: 6. Scope: where in the program it can be n Can have a nameless (anonymous) memory accessed cells. Chapter 5: Variables 9 Chapter 5: Variables 10 Names Names: length We discuss all user-defined names here. If too short, they may not convey the meaning of There are some clear design issues to the variable. consider: It too long, the symbol table of the compiler might become too large. n Maximum length? Language examples: n Notation? n FORTAN I: maximum 6 n Are names case sensitive? n COBOL: maximum 30 n Are special words reserved words or n FORTAN 90 and ANSI C: maximum 31 keywords? n Ada and Java: no limit and all are significant n C++: no limit, but implementers often impose one Chapter 5: Variables 11 Chapter 5: Variables 12 2 Names: notation Names: “standard” notation Variables can consist of one or more Some standards can be applied to how letters, numbers (as long as a number is variables are named when one word is not the first character), and an underscore used to describe a variable. character (the underline key.) Camel notation <ident> ::- <letter> { <letter> | <digit> | ’_’ } n Uses capital letters to indicate the break Some old languages allowed embedded between words. n Camel is named such because the capital spaces which were ignored letters separating the words look like little n FORTRAN 90: camel humps Sum Of Salaries vs. SumOfSalaries n Example: CostOfItemAtSale Chapter 5: Variables 13 Chapter 5: Variables 14 Names: “standard” notation Names: “standard” notation Underscore notation Hungarian notation n Uses an underscore to separate words that n Uses two letters, both lower-case make up a variable. First letter indicates the scope of the variable n Example: Cost_of_item_at_sale Second letter indicates the type of the variable n Example: l_fCostOfItemAtSale Some other standards are used to identify Prefix notation the data type stored in the variable n Uses a prefix (usually three letters) to indicate the type of variable. n Example: floCostOtItemAtSale Chapter 5: Variables 15 Chapter 5: Variables 16 Variable name Explanation Names: case sensitivity I This is a really bad variable to use. You can't tell what it contains and if anyone wants to fix it later, a simple search and replace will be very tedious since single letters are used in words as well. lastname This is much better but uses no form of notation. FOO = Foo = foo ? LastName This is camel notation Disadvantage: strLastName This is prefix – camel notation. Note that the prefix is in all lower case. Poor readability, since names that look alike last_name This is underscore notation. As with camel notation, you can easily identify the two words to a human are different that make up the variable name Worse in some languages such as Modula-2, str _last_name This is prefix underscore notation. Again, the prefix is in lower case. C++ and Java because predefined names are lcLastName This is Hungarian camel notation. The first two letters tell us what type of variable is mixed case used. In this case, this variable contains a last name, is local to the function/procedure, and is a character string. IndexOutOfBoundsException lc _last_name This is Hungarian underscore notation. Chapter 5: Variables 17 Chapter 5: Variables 18 3 Names: case sensitivity Names: special words Advantages: Used to make programs more readable. Larger namespace Used to name actions to be performed. Ability to use case to signify classes of Used to separate the syntactic entities of variables (e.g. make constants be in upper- programs. case) Keyword C, C++, Java, and Modula-2 names are case sensitive but the names in many other n A word that is special only in certain contexts. languages are not. n Example: in FORTRAN the special word Real can be used to declare a variable, but also as Variable in Prolog have to begin with an a variable itself upper case letter. Chapter 5: Variables 19 Chapter 5: Variables 20 Names: special words Names: special words n Real TotalSale (variable TotalSale is of type Real) Reserved Word n Real = 3.1416 (Real is a variable) n A special word that cannot be used as a n Integer Real (variable Real is of type Integer) user-defined name. n Real Integer (variable Integer is of type Real) n Example: C’s float can be used to declare a Disadvantage: poor readability variable, but not as a variable itself. n Distinguish between names and special words by context. Advantage: flexibility Chapter 5: Variables 21 Chapter 5: Variables 22 Variables: Address Address The memory address with which a variable is associated. A variable may have different addresses at different times during execution n Also called l-value because that is what is required when a variable appears in the LHS n Example: variable X of a subprogram is of an assignment. allocated from the runtime stack with a A variable (identified by its name) may different address each time the subprogram have different addresses at different is called (e.g. recursion). places in a program n Example: variable X is declared in two different subprograms (functions) Chapter 5: Variables 23 Chapter 5: Variables 24 4 #include <stdio.h> // --------- Prototype --------- void foo(); Variables: address void bar(); // ---------- Definition --------- void foo() A schematic representation of a variable { int x; printf(“The address of x in foo() is: %d\n”, &x); can be drawn as: } void bar() { printf(“Called from bar(),”); foo(); } Type // ---------- main ---------- Name Lifetime Value int main() Scope { The address of x in foo() is: 1244964 int i = 0; Called from bar(). The address of x in foo() is: 1244880 Address foo(); bar(); sleep(30000); return 0; } Chapter 5: Variables 25 Chapter 5: Variables 26 Variables: address Variables: address (aliases) Concentrate on name, address and value If two variable names can be used to access the attributes same memory location, they are called aliases. Aliases are harmful to readability n Simplified representation: n Program readers must remember all of them. n They are useful in certain circumstances. Example: Name Value int i, *iptr, *jptr; Address iptr= &i; jptr= &i; n A pointer, when de-referenced (*iptr) and the variable's name (i) are aliases Chapter 5: Variables 27 Chapter 5: Variables 28 type intptr = ^integer; Aliases var x, y: intptr; Aliases can occur in several ways: begin n Pointers new(x); n Reference variables x^ := 1; n Pascal variant record y := x; n C and C++ unions y^ := 2; n FORTRAN equivalence writeln(x^); n Parameters end; Some of the original justifications for aliases are no longer valid; e.g. memory reuse in FORTRAN After the assignment of x to y, y^ and x^ both refer to the same n Replace them with dynamic allocations. variable, and the preceding code prints 2. Chapter 5: Variables 29 Chapter 5: Variables 30 5 After the declarations, both x and y have After the call to new(x), x^ has been been allocated in the environment, but the allocated, and x has been assigned a values of