Examples Of Imperative Programming

1429 Words6 Pages
# What is imperative programming? Imperative programming is a [paradigm](https://mortoray.com/topics/paradigms/) that explicitly tells the computer what to do and how to do it. Unlike most other approaches it is a relatively concrete view on data and execution -- there isn't much mystery or abstract runtime behavior involved. The essential aspects of imperative programming are sequenced instructions and mutable data. Imperative programming is the cornerstone of computing. CPUs primarily work as imperative execution engines, and compilers translate into this language. As the primary way of driving computers for most of our programming history, it's well represented in the language arena. > I'm not saying all of computing works this way. A…show more content…
Imperative programming directly exposes the concept of memory. We get a canvas in which we can read and write values at will. This requires we understand values and pointers. Many languages go so far as allowing us to reinterpret memory as arbitrary types. > Concurrent processing poses a problem to imperative programming. The general paradigm still holds, but the "as-if" rule is significantly loosened across threads. Only special instructions are guaranteed to have any ordering between parallel threads. Data is only required to be consistent if we go out of our way to make it consistent. Failing to pay tribute to the concurrent gods will invite evil spirits that wander the program and smash bits in infuriatingly hard-to-decipher patterns. ### Functions Sequential ordering and mutable data define how functions work. A function may, like it's functional programming cousin, simply take input and provide a calculated result, but in imperative programming it's unlimited in what it can do. A function may have all sorts of side effects, changing global memory values, populating a database, loading a texture for the screen, starting a phone call,…show more content…
C programmers were structuring their code this way before C++ was even available. It's perhaps a refined of the paradigm but not fundamentally different. We're still giving the computer rather explicit instructions on how to structure data and the order in which things should be executed. ## Building blocks Imperative components are linked to each via functions and variables. Programs are created by sequencing calls to many libraries and altering a global state, in memory, on disk, or over a network. We can loop on conditions, branch, or even call functions that wait for something to happen. Everything that happens is explicitly written, and ordered, in the code. Though complete programs can be written strictly using this paradigm, it is no longer common except for the smallest of scripts. Imperative programming is well served when combined with other [paradigms](https://mortoray.com/topics/paradigms/) to create applications. A UI application will be connected with events, where the individual responders are written imperatively. Server deployment will be written with a declarative approach, but the individual rules use imperative code. A simulation package that has primarily functional calculations can be glued together with a bit of imperative
Open Document