115

By researching around (books, Wikipedia, similar questions on SE, etc) I came to understand that Imperative programming is one of the major programming paradigms, where you describe a series of commands (or statements) for the computer to execute (so you pretty much order it to take specific actions, hence the name "imperative"). So far so good.

Procedural programming, on the other hand, is a specific type (or subset) of Imperative programming, where you use procedures (i.e., functions) to describe the commands the computer should perform.

First question: Is there an Imperative programming language which is not procedural? In other words, can you have Imperative programming without procedures?

Update: This first question seems to be answered. A language CAN be imperative without being procedural or structured. An example is pure Assembly language.

Then you also have Structured programming, which seems to be another type (or subset) of Imperative programming, which emerged to remove the reliance on the GOTO statement.

Second question: What is the difference between procedural and structured programming? Can you have one without the other, and vice-versa? Can we say procedural programming is a subset of structured programming, as in the image?

enter image description here

4 Answers 4

68

Many of the terms can be reused (often misused) about programming languages, especially those other than object oriented ones.

Here are some small descriptions of the terms.

  1. Imperative programming - In good old days, when programming was broadly in assembly, code would have tons of GOTOs. Even higher level languages like FORTRAN and BASIC began using the same primitives. In this programming paradigm, the entire program is a single algorithm or complete functionality written linearly - step-by-step. This is imperative style. Understand that one can really write totally bad imperative work even in modern C language as well but it is quite easy to organize code in higher level languages.

  2. Structured and Modular programming - Most often we should be able to use the term interchangeably but with subtle differences. When higher level languages begun to get richer, one realized that all units of work should be broken into smaller tractable parts - that is when functions came into existence and programming became a hierarchy of functions and many at lower level could be re-used.

    • Structured programming is any programming when functionality is divided into units like for loop, while loop, if... then etc block structure.
    • Also, here the a piece of code (function) can be re-used.
    • In modular programming, one can create a physical form of package - i.e. a chunk of code that can be shipped; which are fairly general purpose and re-usable. This is called modules of elements compiled together.
    • So one can hardly see modular programs which are not structured and vice versa; the technical definition is subtly different but mostly structured code can be made modular and other way.
  3. Then came "object oriented programming" which is well defined in literature. Understand that object oriented programming is a form of structured programming by definition. The new name for all those function based code which is structured code but NOT object oriented is often called as Procedural programming.

    • So basically structured code where functions (or procedures) dominate over data is called procedural whereas class and object based representation is called object oriented. Both by definition are also modular.

Many people think - all of structured programming (maybe skipping Object based) as imperative programming; I guess this is only due to lack of clear definition of imperative programming - but it is wrong. You are doing structured programming when you are not doing much imperative ones! But I can still write a lot of functions as well as a lot of goto statements inside C or FORTRAN program to mixup.

To be specific to your questions:

First Question : Pure assembly language is imperative language which is NOT structured or procedural. (Having step by step interpretive control flow doesn't mean procedural - but division of functionality into functions is what makes a language procedural).

  • correction * Most modern forms of assembly DO support the use of functions. In fact, everything that's possible in high level code HAS to exist low level to work. Although it's a far better practice to create procedural code, it's possible to write both procedural and imperative code. Unlike the latter, it's more maintainable and easier to understand (avoiding horrible spaghetti code). I think there are shell/bash scripts that better fit the accolade of being purely imperative, but even then, most have functions, developers definitely understand how much value they have.

Second Question : Procedural programming is a FORM of structured programming.


BONUS

7
  • 1
    So would you say that procedural programming necessarily is also structured programming, while the opposite is not true (though often the case)? Commented Oct 31, 2011 at 18:33
  • 2
    Yes, i would say so. Commented Nov 1, 2011 at 3:10
  • 2
    In my answer i have defined imperative vs. structured - where imperative programming is written with just step-by-step execution and is not structured. However, according to some definition there is another classification; this is a classification between Declarative (or functional language) vs. Imperative. Declarative languages allow computation without describing its control flow where as imperative is where explicit control flow (step-by-step) is defined. Based on this classification, Imperative programing, for some can be a super-set of structured. Some don't quite follow that definition. Commented Nov 1, 2011 at 3:14
  • I have a different opinion, with regards to the definition of structured programming. Structured Programming and Modular Programming are not the same thing. Please see the definition at the end of this note. The same link suggests that Assembler ** is a ** structured programming language! Reference for definition of STP:en.wikipedia.org/wiki/Structured_programming – Emmad Kareem
    – NoChance
    Commented Jan 17, 2012 at 5:43
  • Is a "physical form of package"... a file, or a directory/archive of files? (Or is it not, and it's something else.)
    – n611x007
    Commented Jun 23, 2012 at 18:50
13

I am afraid none of the answers given so far capture the core of the concepts very well.

Imperative, procedural and structured are not mutually exclusive properties, they just focus on different aspects of the logic.

Imperative is the counter part of declarative. Imperative basically means that you tell the computer what to do by having it execute a series of instructions that you provide. A declarative program on the other hand tells what is to be achieved. In other words, it is defining steps versus defining a result.

Procedural programming refers the ability of the processor (either hardware or an interpreter) to wrap up instructions into compounds, jump to such a compound and return to the point after the jump once the compound has been executed. This may sound trivial and by today's standards it is, but you need some basic support in the machine before you can do this: the ability to jump, some sort of stack to push an address on that can be popped and jumped to later and a stack pointer. Micro processors soon offered this feature but you can imagine a primitive processor that is only capable of executing instructions fed to it sequentially, like a punch tape or punch card processor.

Structured programming is the next step up from the ability to jump to another instruction. Ultimately everything comes down to jumps but if you can have conditional jumps, you can build basic control flow statements like if-then, for, while, repeat-until and switch. Applying those is called structured programming.

In any modern programming environment you will have all of the above at your disposal and take them for granted so we do not speak of them as such anymore. The differentiating properties between languages has long shifted to higher level paradigms like object oriented and functional programming.

Declarative programming is still not commonplace though, mainly because it will always be domain specific, at least to some extent. You cannot have a general purpose declarative language. This is why we are still stuck with so called 3rd generation languages (which are imperative), where declarative programming or "modeling" would be considered 4th generation.

6
  • Excellent answer (imperative vs declarative = control vs logic = command vs statement = action vs state = step vs result). So are HTML (e.g. <!DOCTYPE html><html lang=""><title>x</title>), CSS (e.g. h1 { font-size: 3rem; }), SQL (e.g. INSERT INTO relation (x) VALUES (3);), and HTTP (e.g. PUT / HTTP/1.1 Host: example.com {"x": 3}) imperative languages or declarative languages?
    – Géry Ogam
    Commented Jul 20, 2021 at 8:29
  • 1
    @Maggyero I would call both HTML and CSS declarative because of their descriptive nature. They describe a document and how it should be rendered but no steps towards a result are provided. SQL and HTTP on the other hand are purely command based, they tell what is to be done, thus they are imperative. Commented Jul 20, 2021 at 19:09
  • @MartinMaat SQL is definitely not imperative. It's declarative, as every query keyword is actually a function. A query is just a string of functions, making it declarative. You can pass queries into each other like first-class citizens. If it's not strictly functional, it's very close and possibly certain flavors are.
    – Blaisem
    Commented May 31, 2023 at 15:27
  • 1
    @Blaisem Functions are imperative, they are a series of instructions. Commented Jun 1, 2023 at 4:45
  • @MartinMaat A language based on functions is imperative? That's...literally the opposite interpretation of declarative programming and couldn't be more wrong. You should review the wiki page on declarative programming. In fact, I just noticed SQL is listed there as declarative.
    – Blaisem
    Commented Jun 2, 2023 at 10:12
6

First question: Yes, many pure object-oriented languages qualify. While they have methods, which are very close to functions, they view these methods in terms of messages and do not give them enough weight to call the language procedural.

Second question: The difference is often in a different scope. You can have a function with goto statements all over the place, which will be in procedural style, but not structured programming. On the other hand, most OO languages support and encouraged structured programming, but not procedural programming.

Procedural programming describes the global ordering of the program. Procedural programs are those that are most effectively understood by looking at their call graphs. Structural programming is a local property, it applies to the use of if and while as opposed to goto.

As such, these two properties are disjunct, you can have either one without the other.

5
  • 1
    The only litmus test for being Procedural is that the language contain procedures (function or methods) and be Imperative, ie. non-Declarative. This means most OO languages that are not pure functional would be Procedural.
    – snakehiss
    Commented Oct 31, 2011 at 16:15
  • 1
    @dietbuddha: By this definition, Haskell would qualify as a procedural language via its use of monads. I'd require a certain deal of reliance on procedures to make a language procedural.
    – thiton
    Commented Oct 31, 2011 at 16:23
  • I don't know Haskell that well, but I thought Haskell is a pure functional language because it wraps side effects in Monads.
    – snakehiss
    Commented Oct 31, 2011 at 17:27
  • 3
    Well, some people claim Haskell is not purely functional because it allows any interaction with the outside world (or because a GHC extension, unsafePerformIO allows wreaking havok). Others joke that Haskell is their favourite imperative programming language. But fact is, a very large degree of Haskell code lives cleanly seperated from IO, doesn't use nonstandard loopholes to sneak side effects in, and is purely functional.
    – user7043
    Commented Oct 31, 2011 at 17:53
  • 1
    @thiton I disagree. Monads are functional constructs too; they only look imperative due to Haskell's syntax sugar (the "do notation"). When you desugar monads, their functional nature becomes evident. In contrast, OO languages are indeed imperative: at their core they are based on the sequential execution of statements.
    – Andres F.
    Commented Nov 16, 2012 at 16:47
2

most of the popular languages of the past 50 years have been designed around the prevalent computer architecture, called the Von Neumann architecture, after one of its originators, John von Neumann.

These languages are called imperative languages.

In a von Neumaan computer, both data and programs are stored in the same memory. The CPU which executes instructions is separate from the memory. Therefore instructions and data must be transmitted from memory to CPU. Results of operations in the CPU must be moved back to memory. Nearly all digital computers built since 1940s have based on von Neumaan architecture.

3
  • 1
    +1 not sure what this has to do with the question, but it's an interesting answer. Commented Nov 16, 2012 at 17:04
  • 3
    What? Where's the part about Imperative vs Procedural, etc? Commented Mar 8, 2015 at 16:50
  • 1
    I think the point here is that quibbling about program structure (Imperative, Declarative, Procedural, Object-Oriented, Functional, etc) are language constructs, and all programs ultimately are processed on Von Neumann architecture machines. This is similar to saying all Turing Complete languages are equivalent. Commented Mar 29, 2019 at 22:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.