33

I am programming in Java in a very object-oriented (OO) style. OOP comes very intuitively to me, but I have very little knowledge about other kinds of programming.

What exactly is procedural programming? How exactly is it different from OOP? Is it the same thing as functional programming?

I used to think that all programming that isn't OO is procedural. But I'm starting to think this isn't true.

3
  • 9
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Commented Feb 27, 2014 at 12:33
  • 2
    Procedural programming is not the same thing as functional programming; it's actually essentially the same thing as object-oriented programming, minus the objects and classes. Commented Feb 27, 2014 at 16:59
  • 1
    Imperative OOP actually is procedural programming, so it is exactly what you're doing all the time ...
    – Ingo
    Commented Feb 28, 2014 at 11:03

4 Answers 4

70

Wikipedia has good explanations for these terms. Regardless, here's the summary:

  • Imperative programming models computation as a sequence of statements that alter mutable state.
  • Procedural programming is imperative programming that breaks down the code into subroutines.
  • Structured programming is a more disciplined approach to procedural programming that forbids arbitrary jumps (e.g. goto) and global state changes.

  • Declarative programming is the opposite of imperative programming - it specifies what to calculate rather than how (e.g. SQL, regexes).

  • Functional programming models computation as expressions that (may) yield values. Functions are values and can be passed to or returned from other functions. Mutation is discouraged; all variables are immutable by default. As a result, it's more declarative than imperative, since it emphasizes what is being computed rather than the sequence of state changes needed to achieve it.

  • Purely functional programming disallows mutation altogether (though contrary to popular belief still has mechanisms for achieving side effects).
  • Total functional programming additionally forbids exceptions and infinite looping. (A total function in mathematics is a function that returns a value for all of its inputs.)

Their relationships are a bit complicated because OOP is a pretty loaded term. You can use objects in both functional languages and procedural languages, but the languages that advertise themselves as OO are procedural. To further confound the issue:

  • Most people don't know the difference between an object and an abstract data type
  • Mainstream OOP languages make no mention of ADTs, provide very poor support for them, and tout objects as The One True Way.
  • No one says Abstract Data Type-Oriented Programming (because it'd be a silly thing to do; you need both ADTs and objects.)

This causes people to think OOP is the only way to achieve abstraction, and that functional programming and OOP are somehow opposites or mutually exclusive. A lot of people also think all functional languages are pure and disallow mutation.

Additionally, people generally toss around imperative/procedural interchangeably, sometimes contrasting it with OOP (implying abstraction-less code, generally C) and sometimes contrasting it to functional programming. The term structured programming has mostly fallen out of use as far as I can tell (likely because at this point most people take for granted that goto and globals are considered harmful.)

17
  • 3
    "forbids jumps" is rather generic; that includes if/while/etc.. perhaps "forbids arbitrary jumps"?
    – Izkata
    Commented Feb 27, 2014 at 16:08
  • @Izkata Good point, changed.
    – Doval
    Commented Feb 27, 2014 at 16:12
  • 1
    Might be worth actually linking to the wikipedia entries.
    – haylem
    Commented Feb 27, 2014 at 16:59
  • And that's why it's called Object "Oriented" and not Object Only.
    – JeffO
    Commented Feb 27, 2014 at 17:07
  • 1
    @OrangeDog How is that any different from an abstract data type, which also defines an encapsulated set of data and functions that can act on it? Moreover, you can have immutable objects, so in that case, what state?
    – Doval
    Commented Feb 28, 2014 at 14:47
12

Procedural Programming is an approach to programming that is one of the basic of building blocks for many other language designs (functional not being one).

Most languages fall into the set of "Procedural Programming" and it is probably the most natural design approach for most people (if you think in terms of OO, then I'd say you're in a minority).

BASIC is procedural.

As others have said, it is a mechanism for structuring programs in a sequential manner.

  • First I do x
  • Second I do y
  • Thirdly I do Z

It requires a mechanism for defining "procedures" - blocks of named code similar to OO methods, that can accept zero to many parameters, and optionally return a value (which would then generally be called a function - probably leading to your confusion with functional languages)

The paradigm does not dictate what the things you do will be, or the manner of the things being passed around.

It simply describes that the program will be structured as a series of procedures (or functions) that operate in a sequential manner. The data is then defined independently of the procedures.

This differs from object-oriented programming, which structures the program around collections of data and methods (not functions) that act on that data.

One way to think about it is in terms of data scope.

In a procedural language scoping is fairly simple. A variable may be in the scope of a given procedure (declared locally), up to the level of the top thing calling stuff (declared globally), with nested scopes between.

In an object-oriented language you add a new scoping context, being that of the object currently in use, which is orthogonal to the above.

Another way to think of procedural, as compares to object-oriented is to consider an object-oriented language where all methods must be declared as static. The result is a procedural language where classes can be used to group procedures together.

5

Procedural Programming is definitely not functional programming.

Procedural programming is when you have a model of the computer as a machine in your head, and you're thinking about how it's modifying data in memory. So first you set A to the value 3, then you add 1 and store that in memory location A again (overwriting the previous value).

Functional programming would be saying A is 3, and B is A + 1, and then letting the computer figure out how to calculate B. Once you've defined A it should be immutable (non-changing). Functional also allows you to do things like pass a function around as a first-class value (a function can take a function as an argument).

Object-oriented programming often combines both, and is kind of orthogonal to both. You can use functional programming and return an immutable object, and that object can have a method that returns some calculated value, and even do it lazily - that's functional object oriented programming. You can also have an object that represents a "repository" (abstract version of a database), and you can "save" stuff in the repository and "get" stuff back out, and let that object handle all the details of how that's done. That's basically object oriented procedural programming.

1

OOP is nothing than a bit refined form of procedural programming, which again belongs to the greater family of imperative programming. Proof of that claim is that many C#/Java programmers tend to "do something" and prefer methods like:

void doThisAndThat(....) { ... do something ... }

So, a program that consist of a bunch of void-methods (formerly known as procedures (sic!)) and code like:

doThis();
if (state is that) doSomethingElse();
doThat();

is perfect procedural programming.

4
  • doThisAndThat(....) implies that a method will do more than one thing which is not in general a good practice. Java and C# developers mostly adhere to the Single Responsibility Principle. I think your analogy is flawed. objectmentor.com/resources/articles/srp.pdf
    – JohnK
    Commented Feb 28, 2014 at 16:57
  • 1
    @JohnK I know that it is no good practice. Yet a common one. Especially among Java developers, if one can judge by what one sees everyday on SO.
    – Ingo
    Commented Feb 28, 2014 at 17:20
  • @JohnK Java and C# developers mostly adhere to the Single Responsibility Principle - Lip service?
    – Ingo
    Commented Feb 28, 2014 at 17:22
  • 1
    Java developers mostly adhere to the Single Responsibility? If only that were true in real life... Commented Jul 22, 2014 at 18:37

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