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

Dipan Mehta
  • 10,612

4 Answers4

69

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

Dipan Mehta
  • 10,612
14

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.

Martin Maat
  • 18,652
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.

thiton
  • 5,348
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.

Mason Wheeler
  • 83,213