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

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/230604

  •  02-10-2020
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
scroll top