02/02/24 05:45 CP 111: Principles of Programming 1
Introduction Imperative languages are abstractions of von Neumann architecture Memory Processor Variables characterized by attributes To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility.
02/02/24 05:45 CP 111: Principles of Programming 2
Names Names denotes the character sequence used for identifiers which denote variables, types, functions, and other entities in source code and documentation. Variable names are the most common names in programs.
02/02/24 05:45 CP 111: Principles of Programming 3
Names Design issues for names: Maximum length? Are connector characters allowed? Are names case sensitive? What are the special words, reserved words or keywords?
02/02/24 05:45 CP 111: Principles of Programming 4
Names Length: If the length is too short, names will have reduced power of implying or suggesting something in addition to what is explicit. Examples: Pascal: 8 characters FORTRAN 95: maximum of 31 characters C99: no limit but only the first 63 are significant; also, external names are limited to a maximum of 31 C#, Ada, and Java: no limit, and all are significant C++: no limit, but implementers often impose one.
02/02/24 05:45 CP 111: Principles of Programming 5
Names Special character: is a character that is not an alphabetic or numeric character. PHP: all variable names must begin with dollar signs ($). Perl: all variable names begin with special characters, which specify the variable’s type Ruby: variable names that begin with @ are instance variables; those that begin with @@ are class variables
02/02/24 05:45 CP 111: Principles of Programming 6
Names Case sensitivity: defines whether uppercase and lowercase letters are treated as distinct (case- sensitive) or equivalent (case-insensitive). Disadvantage: readability (names that look alike are different). Names in the C-based languages are case sensitive. Names in others are not. Worse in C++, Java, and C# because predefined names are mixed case (e.g. IndexOutOfBoundsException).
02/02/24 05:45 CP 111: Principles of Programming 7
Names Special words An aid to readability-used to delimit or separate statement clauses. A keyword is a word that is special only in certain contexts, e.g., in Fortran Real VarName (Real is a data type followed with a name, therefore Real is a keyword) Real = 3.4 (Real is a variable) Disadvantage: poor readability. Compilers and users must recognize the difference.
02/02/24 05:45 CP 111: Principles of Programming 8
Names Special words A reserved word is a special word that cannot be used as a user-defined name. Potential problem with reserved words: If there are too many, many collisions occur (e.g., COBOL has 300 reserved words!) As a language design choice, reserved words are better than keywords. Ex: In Fortran, one could have the statements Integer Real // keyword “Integer” and variable “Real” Real Integer // keyword “Real” and variable “Integer”
02/02/24 05:45 CP 111: Principles of Programming 9
Variables A variable is an abstraction of a memory cell(s). Variables can be characterized as a sextuple of attributes: Name Address Value Type Lifetime Scope
02/02/24 05:45 CP 111: Principles of Programming 10
Variable Attributes Name Not all variables have names: Anonymous, heap- dynamic variables.
02/02/24 05:45 CP 111: Principles of Programming 11
Variable Attributes Address The memory address with which it is associated. A variable may have different addresses at different times during execution. If a subprogram has a local variable that is allocated from the run time stack when the subprogram is called, different calls may result in that variable having different addresses.
02/02/24 05:45 CP 111: Principles of Programming 12
Variable Attributes Address A variable may have different addresses at different places in a program. Aliases If two variable names can be used to access the same memory location, they are called aliases. Aliases are created via pointers, reference variables, C and C++ unions. Aliases are harmful to readability (program readers must remember all of them).
02/02/24 05:45 CP 111: Principles of Programming 13
Variable Attributes Type Determines the range of values of variables. The int type in Java specifies a value range of- 2147483648 to 2147483647 The set of operations that are defined for values of that type. The int type in Java allows arithmetic operations for addition, subtraction, multiplication, division, and modulus. In the case of floating point, type also determines the precision. Represents real numbers approximately, using an integer with a fixed precision. 02/02/24 05:45 CP 111: Principles of Programming 14 Variable Attributes Value Refers to the contents of the memory cell or cells associated with the variable. Abstract memory cell - the physical cell or collection of cells associated with a variable. A variable’s value is sometimes called its r-value because that is what is required when a variable appears in the right side of an assignment statement. The l-value of a variable is its address because it appears on the left side of the assignment statement.
02/02/24 05:45 CP 111: Principles of Programming 15
The Binding Concept A binding is an association, such as between an attribute and an entity, or between an operation and a symbol. Binding time is the time at which a binding takes place. Binding and binding times are prominent concepts in the semantics of programming languages.
02/02/24 05:45 CP 111: Principles of Programming 16
The Binding Concept Possible binding times: Language design time: bind operator symbols to operations. For example, the asterisk symbol (*) is bound to the multiplication operation. Language implementation time: A data type such as int in C is bound to a range of possible values.
02/02/24 05:45 CP 111: Principles of Programming 17
The Binding Concept Possible binding times: Compile time: bind a variable to a particular data type at compile time. Load time: bind a variable to a memory cell (ex. C static variables) Runtime: bind a non-static local variable to a memory cell.
02/02/24 05:45 CP 111: Principles of Programming 18
Static and Dynamic Binding Static Binding First occurs before run time and remains unchanged throughout program execution. Also called early binding or compile time binding Dynamic Binding First occurs during execution or can change during execution of the program
02/02/24 05:45 CP 111: Principles of Programming 19
Type Bindings Before a variable can be referenced in a program, it must be bound to a data type. How is a type specified? When does the binding take place? If static, the type may be specified by either an explicit or an implicit declaration
02/02/24 05:45 CP 111: Principles of Programming 20
Explicit/Implicit Declaration An explicit declaration is a program statement used for declaring the types of variables. Example in C++: char grade; An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program). FORTRAN, BASIC, and Perl provide implicit declarations (Fortran has both explicit and implicit).
02/02/24 05:45 CP 111: Principles of Programming 21
Explicit/Implicit Declaration In Fortran, an identifier that appears in a program that is not explicitly declared is implicitly declared according to the following convention: I, J, K, L, M, or N or their lowercase versions is implicitly declared to be Integer type Otherwise, it is implicitly declared as Real type. Advantage: writability. Disadvantage: reliability suffers because they prevent the compilation process from detecting some typographical and programming errors.
02/02/24 05:45 CP 111: Principles of Programming 22
Dynamic Type Binding Examples of languages that support Dynamic Type Binding are JavaScript and PHP. Specified through an assignment statement e.g., JavaScript list = [2, 4.33, 6, 8]; ⇾single-dimensioned array list = 17.3; ⇾scalar variable Advantage: flexibility (generic program units)
02/02/24 05:45 CP 111: Principles of Programming 23
Dynamic Type Binding Disadvantages: High cost (dynamic type checking and interpretation). Dynamic type bindings must be implemented using pure interpreter not compilers. Pure interpretation typically takes at least ten times as long as to execute equivalent machine code. Type error detection by the compiler is difficult because any variable can be assigned a value of any type.
02/02/24 05:45 CP 111: Principles of Programming 24
Variable Attributes Type inferencing (ML, Miranda, and Haskell) Rather than by assignment statement, types are determined (by the compiler) from the context of the reference. Example: fun circumf(r) = 3.14159 * r * r; The argument and functional value are inferred to be real. fun times10(x) = 10 * x; The argument and functional value are inferred to be int.
02/02/24 05:45 CP 111: Principles of Programming 25
Variable Attributes Storage Bindings & Lifetime Allocation: getting a cell from some pool of available cells. Deallocation: putting a cell back into the pool. Variable lifetime: it is the time during which it is bound to a particular memory cell. The lifetime of a variable begins when it is bound to a specific cell. It ends when it is unbound from that cell.
02/02/24 05:45 CP 111: Principles of Programming 26
02/02/24 05:45 CP 111: Principles of Programming 27
Variable Attributes Static variables Bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. Example: all FORTRAN 77 variables, C static variables. Advantages of Static Variables: History-sensitive: have variables retain their values between separate executions of the subprogram.
02/02/24 05:45 CP 111: Principles of Programming 28
Variable Attributes Advantages of Static Variables: Efficiency (direct addressing): All addressing of static variables can be direct. No run-time overhead is incurred for allocating and deallocating variables. Disadvantages of Static Variables: Storage cannot be shared among variables. Example: If two large arrays are used by two subprograms, which are never active at the same time, they cannot share the same storage for their arrays.
02/02/24 05:45 CP 111: Principles of Programming 29
Variable Attributes Stack Dynamic Variables: Storage bindings are created for variables when their declaration statements are elaborated, but whose types are statically bound. Elaboration of such a declaration refers to the storage allocation and binding process indicated by the declaration Takes place when execution reaches the code to which the declaration is attached.
02/02/24 05:45 CP 111: Principles of Programming 30
Variable Attributes Stack Dynamic Variables Example: The variable declarations that appear at the beginning of a Java method are elaborated when the method is invoked. The variables defined by those declarations are deallocated when the method completes its execution. Stack-dynamic variables are allocated from the run-time stack. If scalar, all attributes except address are statically bound. Local variables in C subprograms and Java methods. 02/02/24 05:45 CP 111: Principles of Programming 31 Variable Attributes In Java, C++, and C#, variables defined in methods are by default stack-dynamic. Advantages of Stack Dynamic Variables: Allows recursion: each active copy of the recursive subprogram has its own version of the local variables. In the absence of recursion it conserves storage because all subprograms share the same memory space for their local variables.
02/02/24 05:45 CP 111: Principles of Programming 32
Variable Attributes Disadvantages of Stack Dynamic Variables: Overhead of allocation and deallocation. Subprograms cannot be history sensitive. Inefficient references (indirect addressing) is required because the place in the stack where a particular variable will reside can only be determined during execution.
02/02/24 05:45 CP 111: Principles of Programming 33
Variable Attributes Explicit Heap Dynamic Variables: Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution. Nameless memory cells. Referenced only through pointers or references. Example: dynamic objects in C++ (via new and delete), all objects in Java.
02/02/24 05:45 CP 111: Principles of Programming 34
Variable Attributes Explicit Heap Dynamic Variables-Advantages and Disadvantages: Advantage: Provides for dynamic storage management. Disadvantage: Inefficient “Cost of allocation and deallocation” and unreliable “difficulty of using pointer and reference variables correctly”.
02/02/24 05:45 CP 111: Principles of Programming 35
Variable Attributes Implicit Heap-dynamic Variables: Bound to heap storage only when they are assigned value. Allocation and deallocation caused by assignment statements. All their attributes are bound every time they are assigned. Example: all variables in APL; all strings and arrays in Perl and JavaScript.
02/02/24 05:45 CP 111: Principles of Programming 36
Variable Attributes Implicit Heap-dynamic Variables-advantages and disadvantages: Advantage: Flexibility allowing generic code to be written. Disadvantages: Inefficient, because all attributes are dynamic “run- time.” Loss of error detection by the compiler.
02/02/24 05:45 CP 111: Principles of Programming 37
Type Checking Type checking is the activity of ensuring that the operands of an operator are of compatible types. A compatible type is: One that is either legal for the operator. Or is allowed under language rules to be implicitly converted, by compiler-generated code, type to a legal type. This automatic conversion is called a coercion or type casting. Example: an int variable and a float variable are added in Java, the value of the int variable is coerced to float and a floating-point is performed.
02/02/24 05:45 CP 111: Principles of Programming 38
Type Checking A type error is the application of an operator to an operand of an inappropriate type. Example: in C, if an int value was passed to a function that expected a float value. a type error would occur (compilers didn’t check the types of parameters). If all type bindings are static, nearly all type checking can be static. If type bindings are dynamic, type checking must be dynamic and done at run-time.
02/02/24 05:45 CP 111: Principles of Programming 39
Strong Typing A programming language is strongly typed if type errors are always detected. It requires that the types of all operands can be determined, either at compile time or run time. Advantage of strong typing: Allows the detection of the misuses of variables that result in type errors. Examples of strongly typed languages: Java and C#. Allows explicit type casting Although type casting can cause type errors, there are no implicit ways they can go undetected. 02/02/24 05:45 CP 111: Principles of Programming 40 Strong Typing The coercion rules of a language have an important effect on the value of type checking. Coercion results in a loss of part of the reason of strong typing – error detection. Example: int a, b; float d; a + d; // the programmer meant a + b, however The compiler would not detect this error. Variable a would be coerced to float.
02/02/24 05:45 CP 111: Principles of Programming 41
Scope The scope of a variable is the range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that statement. Local variable is local in a program unit or block if it is declared there. Non-local variable of a program unit or block are those that are visible within the program unit or block but are not declared there.
02/02/24 05:45 CP 111: Principles of Programming 42
Static Scope Binding names to non-local variables is called static scoping. A compiler for static-scoped language finds a reference to a variable.. The attributes of the variable are determined by finding the statement in which it was declared. Example: Suppose a reference is made to a variable x in subprogram Sub1. The correct declaration is found by first searching the declarations of subprogram Sub1.
02/02/24 05:45 CP 111: Principles of Programming 43
Static Scope If no declaration is found for the variable there.. The search continues in the declarations of the subprogram that declared subprogram Sub1 Called its static parent. The search continues to the next larger enclosing unit (the unit that declared Sub1’s parent), and so forth, until a declaration for x is found or the largest unit’s declarations have been searched without success. An undeclared variable error has been detected.
02/02/24 05:45 CP 111: Principles of Programming 44
Blocks Blocks allows a section of code to have its own local variables whose scope is minimized. Such variables are stack dynamic, so they have their storage allocated when the section is entered and deallocated when the section is exited. Example in C and C++: for (...) { int index; ... } 02/02/24 05:45 CP 111: Principles of Programming 45 Dynamic Scope Based on calling sequences of program units, not their textual layout (temporal versus spatial). Thus the scope is determined at run time. References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point.
02/02/24 05:45 CP 111: Principles of Programming 46
Global Scope Global scope occurs when a variable is defined “outside of a function”. A variable with global scope it is available to all of the functions within your source code. C, C++, PHP, and Python support a program structure that consists of a sequence of function definitions in a file. This allows variable declarations to appear outside function definitions.
02/02/24 05:45 CP 111: Principles of Programming 47
Scope vs Lifetime Scope and lifetime are sometimes closely related, but are different concepts. Example The scope of sum in contained within compute. The lifetime of sum extends over the time during which printheader executes.
02/02/24 05:45 CP 111: Principles of Programming 48
Scope vs Lifetime Example Whatever storage location sum is bound to before the call to printheader, that binding will continue during and after the execution of printheader.
02/02/24 05:45 CP 111: Principles of Programming 49
Reference Environments It is the collection of all names that are visible in the statement. In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes. The referencing environment of a statement is needed while that statement is being compiled Enables code and data structures to be created to allow references to non-local variables in both static and dynamic scoped languages.
02/02/24 05:45 CP 111: Principles of Programming 50
Reference Environments A subprogram is active if its execution has begun but has not yet terminated. In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms.
02/02/24 05:45 CP 111: Principles of Programming 51
Reference Environments Point Referencing Environment in the program 1 local a and b (of sub1), global g for reference, but not for assignment. 2 local c (of sub2), global g for both reference and for assignment 3 non-local c (of sub2), local g (of sub3)
02/02/24 05:45 CP 111: Principles of Programming 52
Named Constants Named constant: It is a variable that is bound to a value only at the time it is bound to storage Its value can’t be change by assignment or by an input statement. The binding of values to named constants can be either static (called manifest constants) or dynamic Advantages: readability and modifiability Example C++: const float PI=3.14;
02/02/24 05:45 CP 111: Principles of Programming 53
Variable Initialization The binding of a variable to a value at the time it is bound to storage is called initialization. Initialization is often done on the declaration statement. Example in C++: int umri=18;
02/02/24 05:45 CP 111: Principles of Programming 54