Definition

What is imperative programming?

Imperative programming is a software development paradigm where every step is implicitly coded to solve a problem. In imperative programming, every operation is coded and the code itself specifies how the problem is to be solved. Most traditional programing languages are imperative.

Imperative programming requires an understanding of the functions necessary to solve a problem, rather than a reliance on models that are able to solve it. The focus of imperative programming is how the problem should be solved, which requires a detailed step-by-step guide. Because the written code performs the functions instead of models, the programmer must code each step. Procedural and object-oriented programming (OOP) languages fall under imperative programming, such as C, C++, C# and Java.

Object in object-oriented programming graphic.
Object-oriented languages fall under the umbrella of imperative programming.

Imperative vs. declarative programming

Imperative programming contrasts with declarative programming. Declarative programming focuses on what needs to be solved instead of how. Declarative programming provides a constant to check to ensure the problem is solved correctly, but does not provide instructions on how to solve the problem. The exact manner in which the problem is solved is defined by the programming language's implementation through models. Declarative programming is also called model-based programming. Functional, domain-specific (DSL) and logical programming languages fit under declarative programming, such as Structured Query Language (SQL), Haskell, HTML and Terraform/OpenTofu.

A simplified example to distinguish between an imperative and declarative approach is to think of giving driving directions. An imperative approach would provide step-by-step instructions on how to arrive at a given destination. A declarative approach would provide the address of the destination, without concern about how it's found.

The models from which declarative programming gets its functions are usually created through imperative programming. As better methods for functions are found through imperative programming, they can be packaged into models to be called upon by declarative programming.

Types of imperative programming models

Imperative programming simply means using a series of steps to get to the desired end. Many more programming paradigms have been created to extend their usefulness and efficiency.

Structured programming adds simple logical structures to imperative programming, such as do-while, for and loops. These simple structures removed the need for repeated confusing goto statements, which were used by early languages.

Modular programming creates code that is repeatable and designed to be reused. It creates modules that other programs or sections of code can call with defined inputs and outputs.

Procedural programming uses subroutines or functions to divide up the program and make it easier to program and to maintain. The use of functions can extract away some of the complicated code structures and help with readability. These functions can also be kept in other modules and imported into new code. It's an integral part of DRY (don't repeat yourself) programming.

Object-oriented programming extends functions and data structures into objects. These objects represent programmed classes and are highly reusable and can be extended.

Imperative programming languages

Most traditional programming languages are considered imperative languages, including the following:

Since declarative programming has many advantages, some imperative languages can also be extended with declarative functions, use a mix of declarative and imperative, or can be used in a purely declarative style.

C++ and C# have included standard libraries with declarative functions. LINQ is a declarative sub-language in C# for databases.

JavaScript has been enhanced with declarative structures. Modern frameworks like React use declarative principles to define the result instead of the process of building the page.

Python can be used either imperatively or declaratively. It was designed with an imperative approach, but contains declarative and functional programming methods. It can also be extended with functional libraries and frameworks.

Advantages of imperative programming

Imperative programming is well understood and simple to comprehend. It is the way most programmers start learning how to program. It uses clearly defined, logical steps, which makes it easier to debug and trace a program that has a bug or problem.

Computers are inherently imperative by nature; the CPU works by going through a series of steps. This makes imperative closer to how a computer functions, so imperative code can be faster and more memory efficient to run. It can also be more easily optimized.

Disadvantages of imperative programming

Imperative programming uses mutable states and data. This means that data can be changed during program execution and might not be what the programmer intended. The long list of steps and control calls can be more difficult to read and maintain than equivalent declarative code.

Imperative programming is well-established and accessible, but the declarative model is gaining popularity due to the rising demand for complex, flexible features. Explore how declarative vs. imperative programming compare.

Continue Reading About What is imperative programming?