Imperative Programming
is what most professional programmers use in their day-to-day jobs.
It's the name given to languages like C, C++, Java, COBOL, etc. In imperative programming, you tell the computer what to do. "Computer, add x and y," or "Computer, slap a dialog box onto the screen." And (usually) the computer goes and does it. This is where most of us spend our lives, in looping structures and if-then-else statements and the like.
It's the name given to languages like C, C++, Java, COBOL, etc. In imperative programming, you tell the computer what to do. "Computer, add x and y," or "Computer, slap a dialog box onto the screen." And (usually) the computer goes and does it. This is where most of us spend our lives, in looping structures and if-then-else statements and the like.
Functional Programming
it seeks to describe what you want done rather than specify how you want something done..
It's probably best understood in contrast to imperative programming.
For Example, if you have a list in C and you want to pull out every Nth element, you have to point at the first element, set a counter at one, move to the next element, increment the counter, check to see if you're at the Nth element and so on.
The functional equivalent would be to write a function that recognizes when the size of a list is a multiple of N, and then pass that function to the list, possibly with another snippet of code to hand back the head of the list if your N-recognizer evaluates to true and discarding it if it evaluates to false. The two functions recurs through the list, and finally hand back a list consisting of every Nth element.
The latter method might seem like the more confusing way to go about things, and that's because it is. Functional programming can be a mind-bender, which is one reason why Lisp, Scheme, and Haskell have never really surpassed C, C++, Java and COBOL in commercial popularity. But there are benefits to the functional way. For one, if you can get the logic correct, functional programming requires orders of magnitude less code than imperative programming. That means fewer points of failure, less code to test, and a more productive (and, many would say, happier) programming life. As systems get bigger, this has become more and more important.
Are there more exotic types?
Not yet. There are hybrids between the two of them (like Scala),
but these merely seek to leverage the strengths of both types. Then there's Object-oriented programming, which is really just a new way to organize data in an imperative program. And even with strange new technologies like quantum computing, the (planned-for) underlying languages fall somewhere in the declarative/imperative spectrum.
- following table show which languages support functional programming (by supporting first-class functions) and for which the functional style is the dominant one.
Language | Closures | Functional |
---|---|---|
C | No | No |
Pascal | No | No |
C++ | Yes | No |
Java | Yes | No |
Modula-3 | Yes | No |
Python | Yes | No |
Ruby | Yes | No |
D (2.0) | Yes | No |
Ocaml | Yes | Yes |
Erlang | Yes | Yes |
Haskell | Yes | Yes |
- The following table describes some of the general differences between these two approaches.
Characteristic
|
Imperative approach
|
Functional approach
|
Programmer focus
|
How to perform tasks
(algorithms) and how to track changes in state.
|
What information is
desired and what transformations are required.
|
State changes
|
Important.
|
Non-existent.
|
Order of execution
|
Important.
|
Low importance.
|
Primary flow control
|
Loops, conditionals,
and function (method) calls.
|
Function calls,
including recursion.
|
Primary manipulation
unit
|
Instances of
structures or classes.
|
Functions as
first-class objects and data collections.
|
No comments:
Post a Comment