Incomputer science,imperative programming is aprogramming paradigm ofsoftware that usesstatements that change a process'state. In much the same way that theimperative mood innatural languages expresses commands, an imperative program consists ofcommands for thecomputer to perform. Imperative programming focuses on describinghow a program operates step by step (with general order of the steps being determined insource code by the placement of statements one below the other),[1] rather than on high-level descriptions of its expected results.
The term is often used in contrast todeclarative programming, which focuses onwhat the program should accomplish without specifying all the details ofhow the program should achieve the result.[2]
Procedural programming is a type of imperative programming in which the program is built from one or more procedures (also termedsubroutines 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. Heavy procedural programming, in whichstate changes are localized to procedures or restricted to explicit arguments and returns from procedures, is a form ofstructured programming. Since the 1960s, structured programming andmodular programming in general have been promoted as techniques to improve themaintainability and overall quality of imperative programs. The concepts behindobject-oriented programming attempt to extend this approach.
Procedural programming could be considered a step toward 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 details of how it achieves its result. At the same time, a complete program is still imperative since itfixes the statements to be executed and their order of execution to a large extent.
Rationale and foundations of imperative programming
The programming paradigm used to build programs for almost all computers typically follows an imperative model.[note 1] Digital computer hardware is designed to executemachine code, which is native to the computer and is usually written in the imperative style, although low-level compilers and interpreters using other paradigms exist for some architectures such asLisp machines.
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 usevariables and more complex statements, but still follow the same paradigm.Recipes and processchecklists, while notcomputer 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 imperative paradigm, perform an operation on information located in memory and store the results in memory for later use. High-level imperative languages, in addition, permit theevaluation of complexexpressions, which may consist of a combination ofarithmetic operations andfunction evaluations, and the assignment of the resulting value to memory. Looping statements (as inwhile loops,do while loops, andfor 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 is met.Conditionalbranching 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 an execution sequence to be transferred to another part of a program. These include the jump (calledgoto in many languages),switch, and the subprogram,subroutine, or procedure call (which usually returns to the next statement after the call).
Early in the development ofhigh-level programming languages, the introduction of theblock enabled the construction of programs in which a group of statements and declarations could be treated as if they were one statement. This, alongside the introduction ofsubroutines, enabled complex structures to be expressed by hierarchical decomposition into simpler procedural structures.
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 byJohn Backus atInternational Business Machines (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 acompiled 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 many 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 theoperating system's target language for some computers.MUMPS (1966) carried the imperative paradigm to a logical extreme, by not having any statements at all, relying purely on commands, even to the extent of making the IF and ELSE commands independent of each other, connected only by an intrinsic variable named $TEST.COBOL (1960) andBASIC (1964) were both attempts to make programming syntax look more like English. In the 1970s,Pascal was developed byNiklaus Wirth, andC was created byDennis Ritchie while he was working atBell Laboratories. Wirth went on to designModula-2 andOberon. For the needs of theUnited States Department of Defense,Jean Ichbiah and a team atHoneywell began designingAda 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, 2005, and 2012.
The 1980s saw a rapid growth in interest inobject-oriented programming. These languages were imperative in style, but added features to supportobjects. The last two decades of the 20th century saw the development of many such languages.Smalltalk-80, originally conceived byAlan Kay in 1969, was released in 1980, by the Xerox Palo Alto Research Center (PARC). Drawing from concepts in another object-oriented language—Simula (which is considered the world's firstobject-oriented programming language, developed in the 1960s)—Bjarne Stroustrup designedC++, an object-oriented language based onC. Design ofC++ began in 1979 and the first implementation was completed in 1983. In the late 1980s and 1990s, the notable imperative languages drawing on object-oriented concepts werePerl, released byLarry Wall in 1987;Python, released byGuido van Rossum in 1990;Visual Basic andVisual C++ (which includedMicrosoft Foundation Class Library (MFC) 2.0), released byMicrosoft in 1991 and 1993 respectively;PHP, released byRasmus Lerdorf in 1994;Java, byJames Gosling (Sun Microsystems) in 1995,JavaScript, byBrendan Eich (Netscape), andRuby, by Yukihiro "Matz" Matsumoto, both released in 1995. Microsoft's.NET Framework (2002) is imperative at its core, as are its main target languages,VB.NET andC# that run on it; however Microsoft'sF#, a functional language, also runs on it.
Fortran (1958) was unveiled as "The IBM Mathematical FORmula TRANslating system." It was designed for scientific calculations, withoutstring handling facilities. Along withdeclarations,expressions, andstatements, it supported:
However, non-IBM vendors also wrote Fortran compilers, but with a syntax that would likely fail IBM's compiler.[4] TheAmerican National Standards Institute (ANSI) developed the first Fortran standard in 1966. In 1978, Fortran 77 became the standard until 1991. Fortran 90 supports:
COBOL (1959) stands for "COmmon Business Oriented Language." Fortran manipulated symbols. It was soon realized that symbols did not need to be numbers, so strings were introduced.[5] TheUS Department of Defense influenced COBOL's development, withGrace Hopper being a major contributor. The statements were English-like and verbose. The goal was to design a language so managers could read the programs. However, the lack of structured statements hindered this goal.[6]
COBOL's development was tightly controlled, so dialects did not emerge to require ANSI standards. As a consequence, it was not changed for 15 years until 1974. The 1990s version did make consequential changes, likeobject-oriented programming.[6]
ALGOL (1960) stands for "ALGOrithmic Language." It had a profound influence on programming language design.[7] Emerging from a committee of European and American programming language experts, it used standard mathematical notation and had a readable structured design. ALGOL was first to define itssyntax using theBackus–Naur form.[7] This led tosyntax-directed compilers. It added features like:
block structure, where variables were local to their block
BASIC (1964) stands for "Beginner's All Purpose Symbolic Instruction Code." It was developed atDartmouth College for all of their students to learn.[8] If a student did not go on to a more powerful language, the student would still remember BASIC.[8] A BASIC interpreter was installed in themicrocomputers manufactured in the late 1970s. As the microcomputer industry grew, so did the language.[8]
Statements could be programmed by preceding them with a line number
The 'list' command displayed the program
The 'run' command executed the program
However, the BASIC syntax was too simple for large programs.[8] Recent dialects added structure and object-oriented extensions.Microsoft'sVisual Basic is still widely used and produces agraphical user interface.[9]
C programming language (1973) got its name because the languageBCPL was replaced withB, andAT&T Bell Labs called the next version "C." Its purpose was to write theUNIXoperating system.[10] C is a relatively small language -- making it easy to write compilers. Its growth mirrored the hardware growth in the 1980s.[10] Its growth also was because it has the facilities ofassembly language, but uses ahigh-level syntax. It added advanced features like:
C allows the programmer to control in which region of memory data is to be stored.Global variables andstatic variables require the fewestclock cycles to store. Thestack is automatically used for the standard variabledeclarations.Heap memory is returned to apointer variable from themalloc() function.
Theglobal and static data region is located just above theprogram region. (The program region is technically called thetext region. It's where machine instructions are stored.)
The global and static data region is technically two regions.[11] One region is called theinitializeddata segment, where variables declared with default values are stored. The other region is called theblock started by segment, where variables declared without default values are stored.
Variables stored in theglobal and static data region have theiraddresses set at compile-time. They retain their values throughout the life of the process.
The global and static region stores theglobal variables that are declared on top of (outside) themain() function.[12] Global variables are visible tomain() and every other function in the source code.
On the other hand, variable declarations inside ofmain(), other functions, or within{}block delimiters arelocal variables. Local variables also includeformal parameter variables. Parameter variables are enclosed within the parenthesis of function definitions.[13] They provide aninterface to the function.
Local variables declared using thestatic prefix are also stored in theglobal and static data region.[11] Unlike global variables, static variables are only visible within the function or block. Static variables always retain their value. An example usage would be the function
Thestack region is a contiguous block of memory located near the top memory address.[14] Variables placed in the stack are populated from top to bottom.[14] Astack pointer is a special-purposeregister that keeps track of the last memory address populated.[14] Variables are placed into the stack via theassembly language PUSH instruction. Therefore, the addresses of these variables are set duringruntime. The method for stack variables to lose theirscope is via the POP instruction.
Local variables declared without thestatic prefix, including formal parameter variables,[15] are calledautomatic variables[12] and are stored in the stack.[11] They are visible inside the function or block and lose their scope upon exiting the function or block.
Theheap region is located below the stack.[11] It is populated from the bottom to the top. Theoperating system manages the heap using aheap pointer and a list of allocated memory blocks.[16] Like the stack, the addresses of heap variables are set during runtime. Anout of memory error occurs when the heap pointer and the stack pointer meet.
C provides themalloc() library function toallocate heap memory.[17] Populating the heap with data is an additional copy function. Variables stored in the heap are economically passed to functions using pointers. Without pointers, the entire block of data would have to be passed to the function via the stack.
In the 1970s,software engineers needed language support to break large projects down intomodules.[18] One obvious feature was to decompose large projectsphysically into separatefiles. A less obvious feature was to decompose large projectslogically intoabstractdatatypes.[18] At the time, languages supported concrete (scalar) datatypes likeinteger numbers,floating-point numbers, andstrings ofcharacters. Concrete datatypes have their representation as part of their name.[19] Abstract datatypes arestructures of concrete datatypes — with a new name assigned. For example, alist of integers could be calledinteger_list.
In object-oriented jargon, abstract datatypes are calledclasses. However, aclass is only a definition; no memory is allocated. When memory is allocated to a class, it's called anobject.[20]
Object-oriented languages support a syntax to modelsubset/superset relationships. Inset theory, anelement of a subset inherits all the attributes contained in the superset. For example, a student is a person. Therefore, the set of students is a subset of the set of persons. As a result, students inherit all the attributes common to all persons. Additionally, students have unique attributes that other persons don't have.Object-oriented languages modelsubset/superset relationships usinginheritance.[23]Object-oriented programming became the dominant language paradigm by the late 1990s.[18]
C++ (1985) was originally called "C with Classes."[24] It was designed to expandC's capabilities by adding the object-oriented facilities of the languageSimula.[25]
An object-oriented module is composed of two files. The definitions file is called theheader file. Here is a C++header file for theGRADE class in a simple school application:
// grade.h// -------// Used to allow multiple source files to include// this header file without duplication errors.// See: https://en.wikipedia.org/wiki/Include_guard// ----------------------------------------------#ifndef GRADE_H#define GRADE_HclassGRADE{public:// This is the constructor operation.// ----------------------------------GRADE(constcharletter);// This is a class variable.// -------------------------charletter;// This is a member operation.// ---------------------------intgrade_numeric(constcharletter);// This is a class variable.// -------------------------intnumeric;};#endif
Aconstructor operation is a function with the same name as the class name.[26] It is executed when the calling operation executes thenew statement.
A module's other file is thesource file. Here is a C++ source file for theGRADE class in a simple school application:
// grade.cpp// ---------#include"grade.h"GRADE::GRADE(constcharletter){// Reference the object using the keyword 'this'.// ----------------------------------------------this->letter=letter;// This is Temporal Cohesion// -------------------------this->numeric=grade_numeric(letter);}intGRADE::grade_numeric(constcharletter){if((letter=='A'||letter=='a'))return4;elseif((letter=='B'||letter=='b'))return3;elseif((letter=='C'||letter=='c'))return2;elseif((letter=='D'||letter=='d'))return1;elseif((letter=='F'||letter=='f'))return0;elsereturn-1;}
Here is a C++header file for thePERSON class in a simple school application:
Here is a C++header file for theSTUDENT class in a simple school application:
// student.h// ---------#ifndef STUDENT_H#define STUDENT_H#include"person.h"#include"grade.h"// A STUDENT is a subset of PERSON.// --------------------------------classSTUDENT:publicPERSON{public:STUDENT(constchar*name);~STUDENT();GRADE*grade;};#endif
Here is a C++source file for theSTUDENT class in a simple school application:
// student.cpp// -----------#include"student.h"#include"person.h"STUDENT::STUDENT(constchar*name):// Execute the constructor of the PERSON superclass.// -------------------------------------------------PERSON(name){// Nothing else to do.// -------------------}STUDENT::~STUDENT(){// deallocate grade's memory// to avoid memory leaks.// -------------------------------------------------deletethis->grade;}