One button, one job
Look at a calculator. The + button adds. The x button multiplies. Each button does one job.
You can use them in any order. You can use the answer of one as the input of the next.
Functional design builds programs in this way: from small parts that each do one job.
Key words
| Word | What it means | Example |
|---|---|---|
| Function | A named block of code that does one job. It can take values in and give a value back. | len("cat") |
| Argument | A value you give to a function when you call it. | "cat" in len("cat") |
| Parameter | The name a function uses for the value it is given. | word in def f(word) |
| Statement | One instruction for the computer. | |
| Block | A group of statements that belong together. | |
| Procedure | A sub-routine that does something but gives no value back. | |
| Sub-routine | A named piece of code that you can call. Functions and procedures are sub-routines. | |
| Recursion | A function that calls itself. | |
| Closure | A function that remembers values from where it was made. | |
| First-class function | A function you can keep under a name or pass around like any value. | |
| Higher-order function | A function that takes another function in, or gives one back. | |
| Anonymous function | A function with no name. Python calls it a lambda. | |
| Currying | Turning a function with many arguments into a chain of functions with one argument each. |
The cells students fill in are left blank in this preview.
Modules with a single job
In functional design, the program is a set of modules. Each module does one defined job.
A small module is easy to test. You can reuse it in many places. If something breaks, you know where to look.
Try it: function or procedure?
def cost(price, count): # a function: it returns a value
return price * count
def show_receipt(total): # a procedure: it only prints
print("You pay", total)
total = cost(3, 4)
show_receipt(total)
# Change the price and the count.
The parts of functional design
| Part | What it is | Example |
|---|---|---|
| Statement | One instruction | total = 5 + 3 |
| Block | Statements that belong together | The lines inside an if |
| Argument | A value you give when you call | print("hi") |
| Procedure | Does a job, gives nothing back | A sub-routine that prints a receipt |
| Function | Does a job, gives a value back | A sub-routine that gives back the cost |
The cells students fill in are left blank in this preview.
Quick check: the parts
Have a go
Quick check: the parts
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Ideas from functional programming
Functional programming has some special ideas. Python can do all of them.
There are six: recursion, first-class functions, higher-order functions, anonymous functions, closures and currying.
Run each example. Change it. Watch what happens.
Try it: recursion
def countdown(n):
if n == 0: # the stop rule
print("Go!")
else:
print(n)
countdown(n - 1) # the function calls itself
countdown(3)
# Remove the stop rule and see what happens.
Worked example: factorial
| Call | It needs | It gives back |
|---|---|---|
| factorial(3) | factorial(2), then 3 times that answer | 3 x 2 = 6 |
| factorial(2) | factorial(1), then 2 times that answer | 2 x 1 = 2 |
| factorial(1) | factorial(0), then 1 times that answer | 1 x 1 = 1 |
| factorial(0) | Nothing. This is the stop rule. | 1 |
The cells students fill in are left blank in this preview.