Saturday, January 23, 2010

Imperative languages v non-imperative languages

In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform.
The term is used in opposition to declarative programming, which expresses what needs to be done, without prescribing how to do it in terms of sequences of actions to be taken. Functional and logical programming are examples of a more declarative approach.

Contents



 Imperative, procedural, and declarative programming

Procedural programming is imperative programming in which the program is built from one or more procedures (also known as subroutines or functions). The terms are often used as synonyms, but the use of procedures has a dramatic effect on how imperative programs appear and how they are constructed. Heavily procedural programming, in which state changes are localized to procedures or restricted to explicit arguments and returns from procedures, is known as structured programming. From the 1960s onwards, structured programming and modular programming in general, have been promoted as techniques to improve the maintainability and overall quality of imperative programs. Object-oriented programming extends this approach.[citation needed]
Procedural programming could be considered as a step towards declarative programming. A programmer can often tell, simply by looking at the names, arguments and return types of procedures (and related comments), what a particular procedure is supposed to do - without necessarily looking at the detail of how the procedure achieves its result. At the same time, a complete program is still imperative since it 'fixes' the statements to be executed and their order of execution to a large extent.
Declarative programming is a non-imperative style of programming in which programs describe the desired results of the program, without explicitly listing command or steps that need to be carried out to achieve the results. Functional and logical programming languages are characterized by a declarative programming style.
In a pure functional language, such as Haskell, all functions are without side effects, and state changes are only represented as functions that transform the state. Although pure functional languages are non-imperative, they often provide a facility for describing the effect of a function as a series of steps. Other functional languages, such as Lisp, OCaml and Erlang, support a mixture of procedural and functional programming.
In logical programming languages, programs consist of logical statements, and the program executes by searching for proofs of the statements. As in functional programming languages, some logical programming languages such as Prolog, and database query languages such as SQL, while declarative in principle, also support a procedural style of programming.
Many imperative programming languages (such as Fortran, BASIC and C) were abstractions of assembly language.

 Overview

The hardware implementation of almost all computers is imperative; nearly all computer hardware is designed to execute machine code, which is native to the computer, written in the imperative style. From this low-level perspective, the program state is defined by the contents of memory, and the statements are instructions in the native machine language of the computer. Higher-level imperative languages use variables and more complex statements, but still follow the same paradigm. Recipes and process checklists, while not computer programs, are also familiar concepts that are similar in style to imperative programming; each step is an instruction, and the physical world holds the state. Since the basic ideas of imperative programming are both conceptually familiar and directly embodied in the hardware, most computer languages are in the imperative style.
Assignment statements, in general, perform an operation on information located in memory and store the results in memory for later use. High-level imperative languages, in addition, permit the evaluation of complex expressions, which may consist of a combination of arithmetic operations and function evaluations, and the assignment of the resulting value to memory. Looping statements (such as in while loops, do while loops and for loops) allow a sequence of statements to be executed multiple times. Loops can either execute the statements they contain a predefined number of times, or they can execute them repeatedly until some condition changes. Conditional branching statements allow a sequence of statements to be executed only if some condition is met. Otherwise, the statements are skipped and the execution sequence continues from the statement following them. Unconditional branching statements allow the execution sequence to be transferred to some other part of the program. These include the jump (called "goto" in many languages), SWITCH and the subprogram, or procedure, call (which usually returns to the next statement after the call).

Block structure

Early in the development of high level languages, the introduction of the block enabled the construction of programs in which a group of statements and declarations could be treated as if they were a single statement. This, alongside the introduction of subroutines, enabled complex structure to be expressed by hierarchical decomposition into simpler procedural structures.

History

The earliest imperative languages were the machine languages of the original computers. In these languages, instructions were very simple, which made hardware implementation easier, but hindered the creation of complex programs. FORTRAN, developed by John Backus at IBM starting in 1954, was the first major programming language to remove the obstacles presented by machine code in the creation of complex programs. FORTRAN was a compiled language that allowed named variables, complex expressions, subprograms, and many other features now common in imperative languages. The next two decades saw the development of a number of other major high-level imperative programming languages. In the late 1950s and 1960s, ALGOL was developed in order to allow mathematical algorithms to be more easily expressed, and even served as the operating system's target language for some computers. COBOL (1960) and BASIC (1964) were both attempts to make programming syntax look more like English. In the 1970s, Pascal was developed by Niklaus Wirth, and C was created by Dennis Ritchie while he was working at Bell Laboratories. Wirth went on to design Modula-2, and Oberon. For the needs of the United States Department of Defense, Jean Ichbiah and a team at Honeywell began designing Ada in 1978, after a 4-year project to define the requirements for the language. The specification was first published in 1983, with revisions in 1995 and 2005/6.
The 1980s saw a rapid growth in interest in object-oriented programming. These languages were imperative in style, but added features to support objects. The last two decades of the 20th century saw the development of a considerable number of such programming languages. Smalltalk-80, originally conceived by Alan Kay in 1969, was released in 1980 by the Xerox Palo Alto Research Center. Drawing from concepts in another object-oriented language—Simula (which is considered to be the world's first object-oriented programming language, developed in the late 1960s)—Bjarne Stroustrup designed C++, an object-oriented language based on C. C++ was first implemented in 1985. In the late 1980s and 1990s, the notable imperative languages drawing on object-oriented concepts were Perl, released by Larry Wall in 1987; Python, released by Guido van Rossum in 1990; PHP, released by Rasmus Lerdorf in 1994; Java, first released by Sun Microsystems in 1994 and Ruby, released in 1995 by Yukihiro “matz” Matsumoto. Microsoft's .NET platform (2002) is at core imperative as are its primary target languages, VB.NET and C#.
Category:Procedural programming languages lists additional imperative programming languages.


QUESTION: What is an imperative language?
ANSWER: We can define such languages according to the characteristics that they display:
QUESTION: Out of the five principal programming language paradigms the imperative is the most popular, why?
However, on the downside there is a tendency for programmers to get "bogged down" with control issues.
QUESTION: Compiled or non-compiled?





2. EXAMPLE IMPERATIVE LANGUAGES

All modern imperative languages can trace their origins back to three imperative languages: FORTRAN, ALGOL 60 and COBOL. Consequently these influential languages are often described as foundation languages.
FORTRAN The IBM Mathematical FORmula TRANslating system.
ALGOL 60 ALGOrithmic Language 1960.


COBOL COmmon Business Oriented Language.


Relationships between a number of common imperative (and other) programming languages are shown in the "wiring diagram" presented in Figure 1.


FAMILY TREE OF IMPERATIVE LANGUAGES
Figure 1: Relationship between a number of common imperative programming languages.

A large number of modern imperative languages follow the style orginated in Algol 60. These lanaguages include Ada and Pascal, and can thus be described as Algol style langauges. There is also a significant group of languages that can be described as C style languages, these include the Object Oriented language C++ and Java. The most widely used imperative language found in industry is currently (the year 2000) C, which tends to dominate the market (with C++ and Java catching up!). This is thus one of the languages that will feature in the following pages. So that we can compare C style langauges (and in particularly C) with other imperativeclanguages we will also feature Ada and Pascal which belong to the Algol style of programming language. Pascal is still a popular teaching language, while Ada was for many years the language of choice for U.S. ministry of defence (arguably the world's largest procurer of software) contracts.




3. FEATURES OF IMPERATIVE LANGUAGES






4. STRUCTURED AND MODULAR PROGRAMMING

4.1. STRUCTURED PROGRAMMING:

The 1960s saw remarkable developments in the field of electronics with the result that more and more powerful hardware components could be produced more and more cheaply. In parallel it was assumed that it would similarly be possible to construct ever larger and more complex software. This assumption proved to be quite wrong - programs failed to be ready on time, greatly exceeded their budget, contained many errors and did not fulfil customer expectations. This phenomenon became known as the software crisis. Among the reasons for this crisis was poor project management, and the fact that many programmers considered their programs to be their property. Thus many individual and curious programming styles developed, and it proved difficult to create error-free programs. In order to remedy this, the concept of structured programming was devised in the late 1960s (by Dijkstra and others). The aim of structured programming is to create programs that are:
Structured programming can be said to be a set of rules and recommendations for how "good" programs should be written. It emphasises programming using sequences, conditions and repetition (not jumps and goto statements). These are all constructs which have a predictable logic, i.e. they are entered at the top and exited at the bottom.
The introduction of the concept of structured programming quickly indicated the need for the support of suitable programming languages. It was this need that initiated the development of what became known as structured languages such as Pascal.


4.2. MODULAR PROGRAMMING:

During the 1970s it became clear that even well-structured programs were not enough for mastering the complexity involved in developing a large program system. It was also recognised that it was necessary to support the division of the program into well-defined parts or modules, that could be developed and tested independently of one another, so that several people could work together within one large programming project. Modular programming is thus concerned with the subdivision of programs into manageable "chunks". The concept encompasses ideas about levels of abstraction - modules are usually arranged in a hierarchy of abstraction. It also espouses information hiding - modules should be designed so that information contained within a module is not accessible to other modules that have no need for such information.