Skip to content
DoingCamp CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

Lesson 1 of 20 · about 14 min

Functions are values

In Python a function is a value, just like a number. You can store it in a variable, put it in a list, and pass it to another function. Functions that take functions are called higher-order functions.

🧭 What you need first: Boss level: build a text adventure

The story

Look at three tiny programs:

  • add up 1 + 2 + 3 + … + 10
  • add up 12+22+32+⋯+1021^2 + 2^2 + 3^2 + \cdots + 10^2
  • add up 13+23+33+⋯+1031^3 + 2^3 + 3^3 + \cdots + 10^3

They’re almost identical. The only thing that changes is what you do to each number before adding it. You could copy-paste the loop three times… or you could write one “add up something” function and hand it the “something” as an input.

That’s the big idea of this lesson: a function is a value, so you can pass it around like a number. A recipe that says “cook the vegetables using your chosen method” works whether you hand it “steam,” “roast” or “fry.”

Another way: a power drill

A drill is one tool, but you snap in different bits: a screwdriver bit, a hole-cutting bit, a sanding pad. The drill (higher-order function) does the spinning; the bit (the function you pass in) decides what the spinning does.

Another way: a mail-merge

“For every customer, send them THIS letter.” The “for every customer” part stays the same; the letter is swapped in. The letter is the function being passed.

Play with it

Pass a function in

Step through. Notice the f variable inside summation refers to a whole function, and f(k) calls whichever one was passed in.

Watch the code run, one step at a time

  1. 1def identity(x):
  2. 2 return x
  3. 3
  4. 4def cube(x):
  5. 5 return x * x * x
  6. 6
  7. 7def summation(n, f):
  8. 8 total = 0
  9. 9 for k in range(1, n + 1):
  10. 10 total += f(k)
  11. 11 return total
  12. 12
  13. 13print(summation(3, identity))
  14. 14print(summation(3, cube))

➜ yellow = the line about to run · ✓ = the line that just ran

Name it

The real words

  • A first-class function can be used anywhere a value can: assigned to a name, stored in a list or dictionary, passed as an argument, returned from another function.
  • square (no parentheses) refers to the function object itself. square(3) calls it and gives back the result. Forgetting the brackets is a common bug: print(square) shows <function square …>.
  • A higher-order function takes a function as an argument or returns one. It expresses a general pattern; the function you pass in fills in the details.
  • Python already has higher-order functions built in:
    • sorted(words, key=len) sorts by the result of calling len on each word.
    • max(people, key=get_age) finds the oldest person.
    • map(f, items) and filter(f, items) apply a function across a sequence (lesson 8).
  • This is the start of abstraction over computation: not just naming values (variables) or steps (functions), but naming patterns of computation.
Build it

Write a higher-order function

Write apply_twice(f, x) that returns f(f(x)). Then write count_if(items, test) that returns how many items make test(item) return True.

Challenge

Sort by anything

Using sorted(..., key=some_function), set by_length to the words sorted from shortest to longest, and by_last_letter to the words sorted by their LAST letter.

Remember it

What's the difference between square and square(4)?

Got it?

Finish the lesson to color it in on your map and collect its concept cards.

Did this lesson click?