240 likes | 255 Views
Names & Variables. Introduction Names Variables Concepts of Binding Type Checking Strong Typing Type Compatibility Named Constants Variable Initialization. Introduction.
E N D
Names & Variables • Introduction • Names • Variables • Concepts of Binding • Type Checking • Strong Typing • Type Compatibility • Named Constants • Variable Initialization
Introduction • Programming languages designed based on von Newmann architecture are Imperative Programming languages. The two primary components of Imperative languages are memory (instructions and data) and its processor. • A Variable is characterized based on its type. • Fundamental attributes of variables is Name
Names • Names are also associated with subprograms, formal parameters and other program constructs. • Design Issues: • Are names case-sensitive? • Are the special words of the language reserved words or keywords. • A name is a string of characters used to identify some entity in a program. • Fortran 95 allows up to 31 characters • C 89 had no length limitation • Names in Java, C# and Ada have no length limit. • C++ does not specify a length limit on names, but implementers sometimes do. Because the symbol tables in which identifiers are stored during compilation need not be too large.
Names… • Names in most programming languages have the same form, A letter followed by a string consisting of letters, digits and underscore characters(_). • In C based languages, it is replaced by the so called camel notation, in which all of the starting letters of words of multiple-word name except the first are capitalized (myStack). • In versions of Fortran prior to Fortran 90, names could have embedded spaces, which were ignored. • Ex:- Sum of Salaries , SumofSalaries are same. • In C based languages uppercase and lower case letters in names are distinct, that is names in these languages are case-sensitive.
Names… • Special Words: Special words in programming languages are used to make programs more readable by naming actions to be performed. • A keyword is a word of programming language that is special only in certain contexts. • Fortran is one of the languages whose special words are key words. • In Fortran the word Real, when found at the beginning of a statement and followed by a name is considered as key word that indicates the statement is declarative statement. • If the word Real is followed by the assignment operator, it is considered a variable name. • Ex:- Real Apple • Real =3.4
Names… • A reserved word is a special word of a programming language that cannot be used as a name. • As a language design choice, reserve words are better than keywords because the ability to redefine keywords can be confusing. • Ex:- In Fortran • Integer Real • Real Integer • In addition to the strange appearance of theses declaration statements, the appearance of Real and Integer as variable names elsewhere in the program could be misleading to program readers. • If a language includes a large number of reserved words, the user has difficulty making up names that are not reserved. • Ex:- COBOL has 300 reserved words, and some commonly chosen names by programmers are in the list of reserved words. Ex- LENGTH, BOTTOM and COUNT.
Variables • A program variable is an abstraction of a computer memory cell or collection of cells. • Programmers often think of variables as names for memory locations. But there is much more to variable than just a name. • A variable is a sixtuple of attributes: name, address, value, type, lifetime and scope. • Name: A string of characters used to identify some entity in a program • Address: The address is the machine memory address with which it is associated. • In many languages it is possible for the same variable to be associated with different addresses at different times in the program. • Ex:- If a subprogram has a local variable that is allocated form the run-time stack when the subprogram is called, different calls may result in that variable having different addresses. These are instantiations of the same variable.
Variables… • The address of a variable is some times called its l-value, because the address is what is required when a variable appears in the left side of an assignment. • It is possible to have multiple variables that have the same address. When more than one variable can be used to access the same memory location, they are called aliases. • Two pointer variables are aliases when they point to the same memory location. This kind of aliasing is a side effect of the nature of pointers and references. • The time when a variable becomes associated with an address is very important to understanding of programming languages. • Type: The type of a variable determines the range of values the variable can store and the set of operations that are defined for values of that type.
Variables… • Value: The value of a variable is the content of the memory cell or cells associated with the variable. • A variables value is sometimes called its r-value, because it is what is required when the variable is used on the right side of an assignment statement. • To access the r-values, the l-value must be determined first.
Concepts of Binding • Binding is an association between and attribute and an entity. • The time at which binding takes place is called binding time. • Binding can take place at language design time, language implementation time, compile time, load time, link time or run time. • The asterisk symbol (*) is usually bound to the multiplication operation at language design time. • A data type such as int in C is bound to a range of possible values at language implementation time. • A variable in Java program is bound to a particular data type at compile time.
Concepts of Binding… • A variable may be bound to a storage cell when the program is loaded into memory. • The same binding does not happen until run time in some cases. • A call to library subprogram is bound to the subprogram code a link time . • Binding of Attributes to Variables: A binding is static if it first occurs before run time and remains unchanged through program execution. If the binding first occurs during run time or can change in the course of program execution is called dynamic. • Type Binding • Static Type Binding: An explicit declaration is a statement in a program that lists variable names and specifies that they are particular type.
Concepts of Binding… • An implicit declaration is a means of association variables with types through default conventions. In this case the first appearance of a variable name in a program constitutes its implicit declaration. • Both implicit and explicit declarations create static bindings to types. • Most programming languages designed since the mid 1960 require explicit declarations of all variables (Perl, Java Script, Ruby and ML are some exceptions ) • Fortran, PL/I and BASIC have implicit declaration whose design were done before 1960. • In Fortran: If the identifier begins with one of the letters I,J,K,L,M or N or their lowercase versions, it is implicitly declared to be Integer otherwise it is Real type.
Concepts of Binding… • Dynamic Type Binding: The type of a variable is not specified by a declaration statement, The variable is bound to a type when it is assigned a value in an assignment statement. • In java script and PHP the binding of a variable to a type is dynamic. Ex:- list = [10.25,3.5]; – single dimensional array of length 2. and if list = 47; followed the above example, list would become a scalar variable. • Type Inference: ML is a programming language that supports both functional and imperative programming. ML employs an interesting type inference mechanism, in which the types of most expressions can be determined without requiring the programmers to specify the type of the variables.
Concepts of Binding… • Ex:- fun area(r) = 3.14*r*r; - This specifies a function named area that takes a floating-point argument and produces a floating point result. The types are inferred form the type of the constant in the expression. • Fun time(x) = 10*x; - the argument and functional value are inferred to be of type int. • fun square(x) = x*x; the type of both the parameter and the return value is determined from the * operator in the function definition. Because this is an arithmetic operator. The type of the parameter and the function are assumed to be numeric. In ML the default numeric type is int. • Type inference is also used in the purely functional languages Miranda and Haskell.
Concepts of Binding… • Storage Binding and Lifetime • Static Variables • Stack-Dynamic Variables • Explicit Heap-Dynamic Variables • Implicit Heap-Dynamic Variables
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 to a legal type. • In general subprograms are operators whose operands are their parameters. • The assignment symbol will be thought of as a binary operator, with its target variable and its expression being the operands. • This automatic conversion is called a coercion. • A type error is the application of an operator to an operand of an inappropriate type. • If all the binding of variables to types are static in a language, then type checking can be done statically.
Type Checking… • Dynamic type binding requires type checking at run time, which is called dynamic type checking. • Type checking is complicated when a language allows a memory cell to store values of different types at different times during execution. • Such memory cells can be created with • Ada variant records • Fortran Equivalence • C and C++ unions.
Strong Typing • A programming language is strongly typed if type errors are always detected. This requires that the types of all operands can be determined, either at compile time or at run time. • A strongly typed language also allows the detection at run time of variables that can store values of more than one type • Fortran 95 is not strongly typed because the use of EQUIVALENCE • Ada is nearly strongly typed. Because it allows the programmers to do type checking rules by specifically requesting that type checking be suspended for a particular type conversion. This suspension of type checking can be done only when a generic function Unchecked_Conversion is called. • C and C++ are not strongly typed languages because both include union types, which are not type checked. • ML is strongly typed. • Java and C# are strongly typed . Types can be explicitly cast.
Type Compatibility • The compatibility rules dictate the types of operands that are acceptable for each of the operators. • The rules are called compatibility because in some case the type of an operand can be implicitly converted by the compiler or run-time system to make it acceptable to the operator. • Type compatibility rules are simple and rigid for the predefined scalar types. But in the case of structured types, such as arrays and records the rules are more complex. • Coercion of these types is rare, this type compatibility is type equivalence. • Two types are equivalent if an operand of one type in an expression is substituted for one of the other type, with out coercion. • Type equivalence is a strict form of type compatibility- compatibility without coercion.
Type Compatibility… • There are two approaches to define type equivalence: • Name Type Equivalence: The two variables have equivalent types if they are defined either in the same declaration or in declarations that use the same type name. • Structure Type Equivalence: means that two variables have equivalent types if their types have identical structures. • Name type equivalence is easy to implement but is more restrictive, Under a strict interpretation, a variable whose type is a subrange of the integers would not be equivalent to an integer type variable. • Ex in Ada:- type Indextype is 1..100; • count: Integer; • Index: Indextype; • The types of the variables count and index would not be equivalent.
Type Compatibility… • Structure Type Equivalence is more flexible than name type equivalence, but it is more difficult to implement. • In name type equivalence, only the two type names must be compared to determine equivalence. Where as in structure type equivalence entire structure of the two types must be compared.
Named Constants • A named constant is a variable that is bound to a value only once. • Named constants are useful aids to readability and reliability. Readability can be improved by using pi instead of the constant 3.14 • Another use of named constants is to parameterize a program. • Ex:- • Void example() { • int [ ] intlist = new int [100]; • String[ ] strlist = new string[100]; • for(index=0; index <100;index++){ • … • } • } • When this program must be modified to deal with a different number of data values, all occurrences of 100 must be found and changed. This is tedious and error-pron. An easier and more reliable method is to use a named constant as a program parameter. • Include the statement: final int len =100;
Named Constants… • Fortran 95 allows only constant expression to be used as the values of its named constants • These constant expression can contain previously declared named constants, constant values and operators. • The reason for the restriction to constants and constant expressions in Fortran 95 is that it use static binding of values to named constants. • Named constants in languages that use static binding of values are sometimes called manifest constants. • Ada and C++ allow dynamic binding of values to named constants. • Ex:- const int result = 2 * width +1; • Where the value of width must be visible when result is allocated and bound to its value. • Java allows dynamic binding of values to named constant. Named constants in java are defined with the final reserved word.
Named Constants… • C# has two kinds of named constants • const • readonly. • Variable Initialization: In many instances it is convenient for variables to have values before the code of the program or sub program in which they are declared begins executing. • The binding of a variable to a value at the time it is bound to storage is called initialization. • Ex:- int sum =0; • int * ptrsum = & sum; • char name[ ] = “named constants”;