Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Functional design

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

50 minutes

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

WordWhat it meansExample
FunctionA named block of code that does one job. It can take values in and give a value back.len("cat")
ArgumentA value you give to a function when you call it."cat" in len("cat")
ParameterThe name a function uses for the value it is given.word in def f(word)
StatementOne instruction for the computer.
BlockA group of statements that belong together.
ProcedureA sub-routine that does something but gives no value back.
Sub-routineA named piece of code that you can call. Functions and procedures are sub-routines.
RecursionA function that calls itself.
ClosureA function that remembers values from where it was made.
First-class functionA function you can keep under a name or pass around like any value.
Higher-order functionA function that takes another function in, or gives one back.
Anonymous functionA function with no name. Python calls it a lambda.
CurryingTurning 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?

Code: python
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

PartWhat it isExample
StatementOne instructiontotal = 5 + 3
BlockStatements that belong togetherThe lines inside an if
ArgumentA value you give when you callprint("hi")
ProcedureDoes a job, gives nothing backA sub-routine that prints a receipt
FunctionDoes a job, gives a value backA 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your 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

Code: python
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

CallIt needsIt gives back
factorial(3)factorial(2), then 3 times that answer3 x 2 = 6
factorial(2)factorial(1), then 2 times that answer2 x 1 = 2
factorial(1)factorial(0), then 1 times that answer1 x 1 = 1
factorial(0)Nothing. This is the stop rule.1

The cells students fill in are left blank in this preview.

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.