Lecture 2-2
What is Functional Programming?
In a functional programming language everything is a pure function.
- The program is built out of pure functions.
- Simple functions are combined to build more complex functions.
This very different style still allows you to do anything you could have done in an imperative language.
Building Functions
Every line is built in the form of:
f(x) = <some other function>
Functions are built up from other functions:
f(x) = square(x) + x
g(x) = h(i(x), j(x))
A pure functional program is similar to an imperative language where each subroutine only has one line and immediately returns a value.
What isn’t in Functional Programming?
- Functional programming has no concept of a variable as variables rely on side effects to operate.
- Functional Programming doesn’t allow loops. This is because loops need variables to operate. Recursion is used instead.
- Anything you can do with a loop can also be done with recursion.
- There is no notion of control flow as everything is just function application.
- Control flow is the notion that instructions are followed one at a time in a list.
Functional programming is about passing around your answers.