**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Preview

Start a free Courses trial

to watch this video

Functional programming has some pretty important central rules that we should understand.

#### The rules

- Computation is the evaluation of functions
- Programming is done with expressions
- No side-effects from computation
- Functions are first-class citizens
- Functions should be limited in scope and functionality

Okay, let's see if we can set some
ground rules and pick up a couple of
0:00

theoretical concepts before we get
into the nitty gritty of writing code.
0:03

The first two are pretty straightforward.
0:07

Computation is
the evaluation of functions.
0:09

This means that we do our work computing
values and outcomes all in functions.
0:13

We'll try to keep as much of our work
inside of discrete functions as possible.
0:17

To me,
this is easiest to think of as math.
0:22

When we're just talking about it, we take
two and combine it with another two, and
0:24

we get four.
0:27

But in programming we'd be using
an add function that takes
0:28

at least two arguments.
0:31

That function then sends back the four.
0:33

So our computation was done by
evaluating the add function.
0:35

This ties neatly into our next rule.
0:39

Programming is done with expressions.
0:41

We do our work by using expressions,
0:43

passing the output of one function into
another, either through chaining or
0:46

by assigning the output to a variable and
then using that variable.
0:50

Again, we can go back to math.
0:54

If we have an equation of 5 minus 3 times
2, that of course has subtraction and
0:55

multiplication in it.
1:00

And according to the order of operations
in math, we do the multiplication and
1:02

use its resulting value in the subtraction
to get us our answer of minus 1.
1:06

And just like doing the same math
problem over and over again will
1:09

always get you the same answer, we should
have no side effects from computation.
1:12

We don't want to change values that
are outside of our function's scope.
1:18

We have to be careful with this when
we're using mutable types like lists and
1:22

dictionaries.
1:26

Eliminating side effects means that
we can run a function with the same
1:27

inputs multiple times and
nothing else, no other inputs or
1:30

values anywhere in the stack will change.
1:33

It also means that we can more easily
predict the state of the entire program
1:35

at any point in time.
1:39

Functions are first-class citizens.
1:40

In Python, functions can be provided
as an argument to a function call or
1:44

returned from a function.
1:47

Since functions are usable as
values just like variables are,
1:49

they're considered to be first-class.
1:52

To get the most out of
functional programming,
1:55

a language needs to be able to do more
with functions than just call them.
1:57

Being able to chain functions together,
take functions as arguments, and
2:00

return functions as values gives us
some fluidity to how we solve a problem.
2:04

Functions should be limited in scope and
functionality.
2:08

We want to make sure our functions
have a well-defined purpose.
2:12

We want them to achieve a single task and
then give us the answer from that task.
2:15

By limiting the scope, or
amount of data that a function has, and
2:19

the functionality of the function,
we can keep our code clean, small, and
2:23

effective, all great things.
2:27

Okay, the next video will
actually get us some practice
2:30

to go along with all this theory.
2:32

I'll see you there in a bit.
2:34

You need to sign up for Treehouse in order to download course files.

Sign up