The Differences Between Imperative and Functional Programming. Part 1.
This semester I'm taking a class called "Principles of Programming Languages." It is the class in at my university where people are first exposed to functional programming (assuming they don't have prior experience).
So far I am really enjoying it. The professor is really engaging and his lectures are really interesting. We are using three books:
- Essentials of Programming Languages
- The classic: The Little Schemer
- And even more of a classic: Structure and Interpretation of Computer Programs
I am having a lot of fun finally reading The Little Schemer. It has been on my to-read list for awhile now. I'm also really enjoying reading SICP again. Back in high school, I read the first few chapters while watching the lectures given back in 1986.
Reading through it now, I've realized that a lot of it went over my head when I was younger. I think I have a better appreciation for it now however.
I wanted to learn a bit about what the differences between imperative languages and functional languages are so I figured I'd write up what I learned. Let's dig in.
A programming paradigm is a way of thinking and seeing the world when it comes to programming. It borrows the Thomas Kuhn definition of paradigm and just applies it to the practice of programming.
The four main programming paradigms (according to Wikipedia) are: imperative, functional, object-oriented, and logic. Although object-oriented is pretty self explanatory, I won't go into it nor will I discuss logic programming.
It is characteristic of a sequence of steps/instructions that happen in order.
Common constructs to manipulate the flow of these instructions include
for loops, functions, and even the dreaded goto (pdf).
Since it is the most dominant programming paradigm, it's what most people are familiar with. Thus if you are anything like me, it just seems like the "natural" way to program.
Since imperative programming is a list of steps that need to be executed, there needs to be some way of keeping track of everything computed to that point. That's (obviously) where variables come in. They are keeping the "state" of where the program is at which then can control where the program should go to next which continues to modify the state.
This sequence of state changes is often called a side effect. The reason it is called a side effect is because it has the potential to modify the state, thus changing the flow of the program.
Functional programming is used in languages like Lisp, Haskell, and Scala.
The main difference between imperative and functional languages is that functions that are evaluated are the main way of gaining and transforming data, functional programming is stateless. Rather than assigning values which can then be mutated like what happens in imperative languages, the value returned by a function is only dependent on its input.
The lack of state allows a functional language to be reasoned just by looking at a function's input and output.
while loops, recursion is used instead. Most
people are exposed to recursion in imperative languages, but functional
languages can have special support for recursion (such as tail call).
Recursion is just the idea of one's own definition to define itself. It may seem
counterintuitive, but it actually is more prevalent than you might expect.
Now that you at least know the definitions of imperative and functional programming, be watching for the second part to this article. It'll include pros and cons and examples of each to illustrate the difference.
When I finish it, I'll link it here.