What's the difference between functional, structured and procedural programming?
Asked Answered
G

3

27

I have read some articles on all subjects, but I don't quite get the differences.

Goa answered 9/12, 2010 at 15:24 Comment(0)
H
46

Structured programming is an old term that I think would encompass functional, procedural, and much else. It basically means using explicit control-flow structures rather than jumping about directly from instruction to instruction.

Functional and procedural programming are both, in that sense, structured paradigms. Functional programming is also declarative programming -- the structure given to your code corresponds to its meaning -- a program is a function that changes the state of the world. Procedural programming is what you'd consider "typical" programming in any C language or its descendants, including OO languages such as Java and C++. A program is a series of instructions, to be executed serially, and invoking subprocedures along the way.

Or, as it has been famously described:

Functional programming is like describing your problem to a mathematician. Imperative programming is like giving instructions to an idiot.

    --- arcus, #scheme on Freenode
Hulking answered 9/12, 2010 at 17:50 Comment(1)
The quote is not really pertinent in helping to solve the OP's original problem, since the quote is about the difference between functional and imperative programming paradigms (yes, even though procedural programming is usually considered a sub-paradigm of imperative programming). Moreover, I would argue that Java does not actually support procedural programming, since in Java everything is a class or an object, except for the new features of Java 8, such as lambda functions, which would be related to functional programming. So, I've to downvote this answer.Manteau
M
16

Functional programming is using functions as first-class elements. Making use of higher order functions (taking and/or returning functions); leading to powerful constructs and well factored code. Some people focus also on the purity aspect of FP which is to say that functions should always return the same result, given the same input. These, I believe, are the two basic pillars of FP. I also see avoiding side effects as essentially abstracting a bit away from the load/store machine level instructions.

Structured programming goes back to Djikstra's "Goto Considered Harmful" paper. It means using if/then/else/elif structures, do/while/until/for loops, etc. instead of resorting to goto. It's essentially abstracting a bit away from the compare/branch machine level instructions. Structured programming is orthogonal to both functional and procedural programming.

Procedural programming, I believe, refers to programming with imperative "subroutines" (as opposed to pure "functions") consisting generally of a series of "statements" (as opposed to "expressions") leaving behind side effects.

Magnetize answered 9/12, 2010 at 18:0 Comment(2)
I would say that procedural programming is a sub-paradigm of imperative programming, as also stated here: en.wikipedia.org/wiki/Imperative_programmingManteau
Very true, I'll delete the "(sometimes 'imperative programming')" aside. Procedural is indeed a subset of imperative. It is quite possible to program imperatively without "procedures" in some languages. However, the key difference between a "procedure" and a "function," I believe, is that the former is imperative. That's what I meant.Magnetize

© 2022 - 2024 — McMap. All rights reserved.