Imperative Programming - Wikipedia
Imperative Programming - Wikipedia
org/wiki/Imperative_programming
Imperative programming
In computer science, imperative programming is a programming paradigm of soware that uses
statements that change a program's state. In much the same way that the imperative mood in natural
languages expresses commands, an imperative program consists of commands for the computer to
perform. Imperative programming focuses on describing how a program operates step by step,[1] rather
than on high-level descriptions of its expected results.
e term is oen used in contrast to declarative programming, which focuses on what the program
should accomplish without specifying all the details of how the program should achieve the result.[2]
Procedural programming
Procedural programming is a type of imperative programming in which the program is built from one
or more procedures (also termed subroutines or functions). e terms are oen 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 which state changes are localized to procedures or
restricted to explicit arguments and returns from procedures, is a form of structured programming.
Since the 1960s, structured programming and modular programming in general have been promoted as
techniques to improve the maintainability and overall quality of imperative programs. e concepts
behind object-oriented programming aempt to extend this approach.
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.
1 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
operations and function evaluations, and the assignment of the resulting value to memory. Looping
statements (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 is met. 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 an execution sequence to be transferred to another part of a
program. ese include the jump (called goto in many languages), switch, and the subprogram,
subroutine, or procedure call (which usually returns to the next statement aer the call).
Early in the development of high-level programming 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 one statement. is, alongside the introduction of subroutines, enabled complex structures to
be expressed by hierarchical decomposition into simpler procedural structures.
Many imperative programming languages (such as Fortran, BASIC, and C) are abstractions of assembly
language.[3]
e 1980s saw a rapid growth in interest in object-oriented programming. ese languages were
imperative in style, but added features to support objects. e last two decades of the 20th century saw
the development of many such languages. Smalltalk-80, originally conceived by Alan 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 first object-oriented programming
language, developed in the 1960s)—Bjarne Stroustrup designed C++, an object-oriented language based
on C. Design of C++ 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 were Perl,
released by Larry Wall in 1987; Python, released by Guido van Rossum in 1990; Visual Basic and Visual
C++ (which included Microso Foundation Class Library (MFC) 2.0), released by Microso in 1991 and
2 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
1993 respectively; PHP, released by Rasmus Lerdorf in 1994; Java, by James Gosling (Sun Microsystems)
in 1995, JavaScript, by Brendan Eich (Netscape), and Ruby, by Yukihiro "Matz" Matsumoto, both
released in 1995. Microso's .NET Framework (2002) is imperative at its core, as are its main target
languages, VB.NET and C# that run on it; however Microso's F#, a functional language, also runs on
it.
Examples
Fortran
FORTRAN (1958) was unveiled as "e IBM Mathematical FORmula TRANslating system." It was
designed for scientific calculations, without string handling facilities. Along with declarations,
expressions, and statements, it supported:
▪ arrays
▪ subroutines
▪ "do" loops
It succeeded because:
▪ records
▪ pointers to arrays
COBOL
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] e US
Department of Defense influenced COBOL's development, with Grace Hopper being a major
contributor. e statements were English-like and verbose. e 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. e 1990s version did make consequential
changes, like object-oriented programming.[6]
Algol
ALGOL (1960) stands for "ALGOrithmic Language." It had a profound influence on programming
language design.[7] Emerging from a commiee of European and American programming language
experts, it used standard mathematical notation and had a readable structured design. Algol was first to
3 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
define its syntax using the Backus–Naur form.[7] is led to syntax-directed compilers. It added
features like:
Basic
BASIC (1964) stands for "Beginner's All Purpose Symbolic Instruction Code." It was developed at
Dartmouth 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 the
microcomputers manufactured in the late 1970s. As the microcomputer industry grew, so did the
language.[8]
Basic pioneered the interactive session.[8] It offered operating system commands within its
environment:
C
C programming language (1973) got its name because the language BCPL was replaced with B, and
AT&T Bell Labs called the next version "C." Its purpose was to write the UNIX operating 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 of assembly language, but uses
a high-level syntax. It added advanced features like:
▪ inline assembler
▪ arithmetic on pointers
▪ pointers to functions
▪ bit operations
▪ freely combining complex operators[10]
4 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
▪ The global and static region stores the global variables that are declared on top of
(outside) the main() function.[12] Global variables are visible to main() and
every other function in the source code.
On the other hand, variable declarations inside of main(), other functions, or within
{ } block delimiters are local variables. Local variables also include formal parameter
variables. Parameter variables are enclosed within the parenthesis of function
definitions.[13] They provide an interface to the function.
▪ Local variables declared using the static prefix are also stored in the global 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 int increment_counter(){ static int
counter = 0; counter++; return counter;}
▪ The stack 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] A stack
pointer is a special-purpose register that keeps track of the last memory address
populated.[14] Variables are placed into the stack via the assembly language PUSH
instruction. Therefore, the addresses of these variables are set during runtime. The
method for stack variables to lose their scope is via the POP instruction.
▪ Local variables declared without the static prefix, including formal parameter
5 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
variables,[15] are called automatic 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.
▪ The heap region is located below the stack.[11] It is populated from the bottom to the
top. The operating system manages the heap using a heap pointer and a list of
allocated memory blocks.[16] Like the stack, the addresses of heap variables are set
during runtime. An out of memory error occurs when the heap pointer and the stack
pointer meet.
C++
In the 1970s, soware engineers needed language support to break large projects down into
modules.[18] One obvious feature was to decompose large projects physically into separate files. A less
obvious feature was to decompose large projects logically into abstract datatypes.[18] At the time,
languages supported concrete (scalar) datatypes like integer numbers, floating-point numbers, and
strings of characters. Concrete datatypes have their representation as part of their name.[19] Abstract
datatypes are structures of concrete datatypes — with a new name assigned. For example, a list of
integers could be called integer_list.
In object-oriented jargon, abstract datatypes are called classes. However, a class is only a definition; no
memory is allocated. When memory is allocated to a class, it's called an object.[20]
Object-oriented imperative languages developed by combining the need for classes and the need for safe
functional programming.[21] A function, in an object-oriented language, is assigned to a class. An
assigned function is then referred to as a method, member function, or operation. Object-oriented
programming is executing operations on objects.[22]
C++ (1985) was originally called "C with Classes."[24] It was designed to expand C's capabilities by
adding the object-oriented facilities of the language Simula.[25]
An object-oriented module is composed of two files. e definitions file is called the header file. Here is
a C++ header file for the GRADE class in a simple school application:
// grade.h
// -------
6 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
class GRADE {
public:
// This is the constructor operation.
// ----------------------------------
GRADE ( const char letter );
A constructor operation is a function with the same name as the class name.[26] It is executed when the
calling operation executes the new statement.
A module's other file is the source file. Here is a C++ source file for the GRADE class in a simple school
application:
// grade.cpp
// ---------
#include "grade.h"
Here is a C++ header file for the PERSON class in a simple school application:
7 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
// person.h
// --------
#ifndef PERSON_H
#define PERSON_H
class PERSON {
public:
PERSON ( const char *name );
const char *name;
};
#endif
Here is a C++ source file for the PERSON class in a simple school application:
// person.cpp
// ----------
#include "person.h"
Here is a C++ header file for the STUDENT class in a simple school application:
// student.h
// ---------
#ifndef STUDENT_H
#define STUDENT_H
#include "person.h"
#include "grade.h"
Here is a C++ source file for the STUDENT class in a simple school application:
// student.cpp
// -----------
#include "student.h"
#include "person.h"
STUDENT::~STUDENT()
{
// deallocate grade's memory
// to avoid memory leaks.
// -------------------------------------------------
delete this->grade;
}
8 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
// student_dvr.cpp
// ---------------
#include <iostream>
#include "student.h"
std::cout
// Notice student inherits PERSON's name
<< student->name
<< ": Numeric grade = "
<< student->grade->numeric
<< "\n";
return 0;
}
# makefile
# --------
all: student_dvr
clean:
rm student_dvr *.o
See also
▪ Functional programming
▪ Comparison of programming paradigms
▪ Reactive programming
▪ History of programming languages
▪ List of imperative programming languages
Notes
1. Reconfigurable computing is a notable exception.
9 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
References
1. Jain, Anisha (2022-12-10). "Javascript Promises— Is There a Better Approach?" (https://
medium.datadriveninvestor.com/javascript-promises-is-there-a-better-approach-dd6a0
a329131). Medium. Retrieved 2022-12-20.
2. "Imperative programming: Overview of the oldest programming paradigm" (https://w
ww.ionos.com/digitalguide/websites/web-development/imperative-programming/).
IONOS Digitalguide. 21 May 2021. Retrieved 2022-05-03.
3. Bruce Eckel (2006). Thinking in Java (https://books.google.com/books?id=bQVvAQAAQB
AJ&q=imperative&pg=PA24). Pearson Education. p. 24. ISBN 978-0-13-187248-6.
4. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 16. ISBN 0-201-71012-9.
5. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 24. ISBN 0-201-71012-9.
6. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 25. ISBN 0-201-71012-9.
7. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 19. ISBN 0-201-71012-9.
8. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 30. ISBN 0-201-71012-9.
9. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 31. ISBN 0-201-71012-9.
10. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 37. ISBN 0-201-71012-9.
11. "Memory Layout of C Programs" (https://www.geeksforgeeks.org/memory-layout-of-c-
program/). 12 September 2011.
12. Kernighan, Brian W.; Ritchie, Dennis M. (1988). The C Programming Language Second
Edition. Prentice Hall. p. 31. ISBN 0-13-110362-8.
13. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 128. ISBN 0-201-71012-9.
14. Kerrisk, Michael (2010). The Linux Programming Interface. No Starch Press. p. 121.
ISBN 978-1-59327-220-3.
15. Kerrisk, Michael (2010). The Linux Programming Interface. No Starch Press. p. 122.
ISBN 978-1-59327-220-3.
16. Kernighan, Brian W.; Ritchie, Dennis M. (1988). The C Programming Language Second
Edition. Prentice Hall. p. 185. ISBN 0-13-110362-8.
17. Kernighan, Brian W.; Ritchie, Dennis M. (1988). The C Programming Language Second
Edition. Prentice Hall. p. 187. ISBN 0-13-110362-8.
18. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 38. ISBN 0-201-71012-9.
19. Stroustrup, Bjarne (2013). The C++ Programming Language, Fourth Edition. Addison-
Wesley. p. 65. ISBN 978-0-321-56384-2.
20. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 193. ISBN 0-201-71012-9.
21. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 39. ISBN 0-201-71012-9.
22. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 35. ISBN 0-201-71012-9.
10 of 11 5/9/24, 21:15
Imperative programming - Wikipedia https://en.wikipedia.org/wiki/Imperative_programming
23. Wilson, Leslie B. (2001). Comparative Programming Languages, Third Edition. Addison-
Wesley. p. 192. ISBN 0-201-71012-9.
24. Stroustrup, Bjarne (2013). The C++ Programming Language, Fourth Edition. Addison-
Wesley. p. 22. ISBN 978-0-321-56384-2.
25. Stroustrup, Bjarne (2013). The C++ Programming Language, Fourth Edition. Addison-
Wesley. p. 21. ISBN 978-0-321-56384-2.
26. Stroustrup, Bjarne (2013). The C++ Programming Language, Fourth Edition. Addison-
Wesley. p. 49. ISBN 978-0-321-56384-2.
Originally based on the article 'Imperative programming' by Stan Seibert, from Nupedia,
licensed under the GNU Free Documentation License.
11 of 11 5/9/24, 21:15