I need as soon as
PowerPoint Presentation School of Science, Engineering and Information Technology Notice for paragraph 135ZXA (a) of the Copyright Act 1968 This Material has been reproduced and communicated to you by or on behalf of Federation University, Australia under Part VB of the Copyright Act 1968 (the Act). The Material in this communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. ITECH5403 - Comparative Programming Languages Week 3 - Names, Bindings and Scopes Al Lansley 2 Introduction • Imperative programming languages are, to varying degrees, abstractions of the underlying von Neumann computer architecture. • The architecture's two primary components are: • Its memory, which stores both instructions and data, and • Its processor, which provides operations for modifying thecontents of the memory. • The abstractions in a language for the memory cells of the machine are variables. • In some cases, the characteristics of the abstractions are very close to the characteristics of the cells; an example of this is an integer variable, which is usually represented direction in one or more bytes of memory. • In other cases, the abstractions are far removed from the organisation of the hardware memory, as with a three-dimensional array, which requires a software mapping function to support the abstraction. 3 Introduction (Cont'd) • A variable can be characterised by a collection of properties, or attributes, the most important of which is type - a fundamental concept in programming languages. • Designing the data types of a language requires that a variety of issues be considered. Amongst the most import of which is the scope and lifetime of variables. • Functional programming languages (Lisp, Scheme, APL etc.) allow expressions to be named - and these named expressions appear like assignments to variable names in imperative languages, but are fundamentally different in that they cannot be changed, so they are like the named constants of the imperative languages. • Pure functional languages do not have variables that are like those of the imperative languages, however many functional languages do include such variables. 4 Introduction (Cont'd) • Families of languages will often be referred to as if they were single languages, for example: • Fortran will mean all of the versions of Fortran, and • Ada will mean all of the versions of Ada. • Likewise, a reference to C will mean the original version of C, as well as C89 and C99. When a specific version of a language is named, it is because it is different from the other family members within the topic being discussed. • In the following discussion, if we add a plus (+) to the name of a version of a language, then we mean all versions of the language beginning with the one named, for example: Fortran 95+ means all version of Fortran beginning with Fortran 95. • Also, the term C-based languages will be used to refer to C, Objective-C, C++, Java and C#. • We will use the terms names and identifiers interchangeably. 5 Names • When designing a programming language, the following are the primary design decisions relating to names (i.e. identifiers / variables): • Are the names case sensitive?, and • Are the special words of the language reserved words or keywords? 6 Name Forms • A name is a string of characters used to identify some entity in a program - quite how a language treats a name varies by language, for example: • Fortran 95+ allows up to 31 characters in its names. • C99 has no length limitation on its internal names, but only the first 63 are significant. • External names in C99 (those defined outside functions, which must be handled by the linker) are restricted to 31 characters. • Names in Java, C# and Ada have no length limit, and all characters in them are significant. • C++ does not specify a length limit on names, although some implementations sometimes do. 7 Name Forms (Cont'd) • Name is most programming languages have the same form: a letter followed by a string consisting of letters, digits and underscores ( _ ). • Although the use of underscores to form names was widely using the 1970s and 1980s, that practice is now far less popular (although it is still commonly used for constants i.e. GRAVITY_EARTH = 9.8f) • The underscore has typically been replaced by specifying names in Camel Case, for example, rather than my_variable_name it is now far more common to see myVariableName. • All variable names in PHP must start with a dollar sign i.e. $myVariableName • In Perl the special character at the beginning of a variable's name ($, @ or %) specifies its type! • In Ruby, special characters at the beginning of a variable's name (@ or @@) indicate that it's an instance or a class variable (i.e. static variable), respectively. Note: CamelCase is so called because the embedded capitals look like a camel's humps… allegedly! 8 Name Forms (Cont'd) • In many languages, notably the C-based languages, uppercase and lowercase letters in names are distinct; that is, names in these languages are case- sensitive. • For example, the following three names are distinct in C++: • rose • Rose • ROSE • To some people, this causes a significant issue with readability, because the names look very similar, but denote completely different entities. • In that sense, case sensitivity violates the design principle that language constructs that look similar should have similar meanings. • However, not everyone agrees that case sensitivity is bad for names! What do you think? Should names be case sensitive or case insensitive - why? 9 Special Words • Special words in programming languages are used to make programs more readable by naming actions to be performed. They are also used to separate the syntactic parts of statements and programs. • In most languages, special words are classified as reserved words - which means they cannot be redefined by programmers …but in some languages special words are merely keywords which can be redefined! • A keyword is a word of a programming language that is special only in certain contexts. • Fortran is the only remaining widely used language whose special words are keywords. In Fortran, the word Integer, when found at the beginning of a statement and followed by a name is considered a keyword that indicates the statement is a declarative statement. However, if the word Integer is followed by the assignment operator, it is considered a variable name. For example: • Integer Apple • Integer = 4 Note: Fortran compilers and people reading Fortran programs must distinguish between names and special words by examining the context in which they are used! 10 Special Words (Cont'd) • A reserved word is a special word of a programming language that cannot be used as a name. • As a language design choice, reserved words are better than keywords because the ability to redefine keywords can be confusing. For example, in Fortran, we could have the following statements. • Integer Real • Real Integer • One potential problem with reserved words is that if a language contains a large number of them then a user may have difficulty making up names that are not reserved! • The best example of this is COBOL, which has 300 reserved words - including amongst them being: LENGTH, DESTINATION, BOTTOM and COUNT! Said nobody ever. I've got an idea! Let's re-assign the Integer and Real keywords so that creating an Integer actually creates a Real, and vice versa - because surely that won't cause me any headaches further down the track!" 11 Variables • A variable is an abstraction of a computer memory cell or collection of cells. Programmers often think of variable names as name for memory locations, but there is much more to a variable than just a name. • The move from machine languages to assembly languages was largely one of replacing absolute numeric memory address for data with names - making programs far more readable and therefore easier to write and maintain. • That step also provided an escape from the problem of manual absolute addressing, because the translator that converted the names to actual addresses also chose those addresses. • A variable can be characterised as a sextuple (i.e. set of 6) of attributes: • Name, • Address, • Value, • Type, • Lifetime, and • Scope. 12 Variable Addresses • The address of a variable is the machine's memory address with which it is associated. • This is not as simple as it may first appear, as in many languages it is possible for the same variable to be associated with different addresses at different times in the program. For example, if a subprogram has a local variable that is allocation from the run-time stack when the subprogram is called, different calls may result in that variable having different addresses! • The address of a variable is sometimes called its l-value ('L' value) - because the address is what is required when the name of a variable appears in the left hand side of an assignment. • It is possible to have multiple variables that have the same address. When more than one variable name can be used to access the same memory location, then the variables are called aliases. 13 Variable Addresses (Cont'd) • Aliasing decreases readability because it allows a variable to have its value changed by an operation performed on a 'different variable'. • For example, if the variables sum and total are aliases, any change to the value of sum affects the value of total, and vice versa! • When reading such a program, the person reading it must remember that sum and total are different names for the same memory cell. And because there can be any number of aliases in a program, this may be very difficult in practice. • Additionally, aliasing makes program verification more difficult. • Aliases can be created in in several different ways - with the most common ones being through the use of: • Pointers, and • References. 14 Variable Types • 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 the type. • For example, the int type in Java specifies: • A value range of -2,147,483,648 up to a maximum value of 2,147,483,647, and •