Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 Feb 27 '14 at 12:33
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. – Mason Wheeler Feb 27 '14 at 16:59
Imperative OOP actually is procedural programming, so it is exactly what you're doing all the time ... – Ingo Feb 28 '14 at 11:03
up vote 66 down vote accepted

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.)

share|improve this answer
"forbids jumps" is rather generic; that includes if/while/etc.. perhaps "forbids arbitrary jumps"? – Izkata Feb 27 '14 at 16:08
@Izkata Good point, changed. – Doval Feb 27 '14 at 16:12
Might be worth actually linking to the wikipedia entries. – haylem Feb 27 '14 at 16:59
And that's why it's called Object "Oriented" and not Object Only. – JeffO Feb 27 '14 at 17:07
@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 Feb 28 '14 at 14:47

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.

share|improve this answer

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.

share|improve this answer

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:

if (state is that) doSomethingElse();

is perfect procedural programming.

share|improve this answer
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. – JohnK Feb 28 '14 at 16:57
@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 Feb 28 '14 at 17:20
@JohnK Java and C# developers mostly adhere to the Single Responsibility Principle - Lip service? – Ingo Feb 28 '14 at 17:22
Java developers mostly adhere to the Single Responsibility? If only that were true in real life... – Sridhar-Sarnobat Jul 22 '14 at 18:37

protected by World Engineer Feb 28 '14 at 3:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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