Lesson 4 — Making your own tools
Step 1: a stretch of code with a name
The toolbox tools were always there. The moment a program grows past a page, you start wanting tools of your own: a stretch of code with a name, that you can hand values to and that hands one value back. That is a function, and make builds one. give is how a function hands its answer back:
The book says it answers:
Hello, Noki. Hello, Ada.
The name in the parentheses is the function's parameter — its own shelf, filled fresh by whoever calls it. One function, any number of calls, each with its own filling.
Your turn. Make a function farewell that is handed a name and gives back "Goodbye, " joined with the name and a full stop. Call it on "Byron".
The worked answer — try yours first, then peek
The book says it answers:
Goodbye, Byron.
Step 2: a function that reckons
The book says it answers:
42 10
Your turn. Make half, which gives back its number divided by 2, and say half(10).
The worked answer — try yours first, then peek
The book says it answers:
5
Step 3: a function with a loop inside
Anything a program can do, a function can do — loops included. This one builds a row of stars of any length:
The book says it answers:
*** *******
Notice let row lives inside the function: each call gets a fresh shelf, so two calls never trip over each other. Names made inside a function's braces belong to that call alone.
Your turn. Make dashes(n) the same way, building a row of dashes, and say dashes(4).
The worked answer — try yours first, then peek
The book says it answers:
----
Step 4: a function that walks a list
The book says it answers:
20 4
Your turn. Make sum(numbers) — the same walk without the dividing — and say sum([1, 2, 3, 4]).
The worked answer — try yours first, then peek
The book says it answers:
10
Putting it together: tools calling on tools
Functions that call on other functions are how big programs are built out of small, trustworthy pieces:
The book says it answers:
************ Average score: 8 ************
You have been writing a library without being told: greet, twice, line, average are your toolbox now, and any program you write can carry them at its top and call on them below. That is the whole secret of large programs — they are small programs, kept in functions, all the way down.
One thing to keep in your pocket: a function that reaches its end without give hands back nothing at all. The next lesson makes that matter.
The ledger grows: the book gets its tools
The book says it answers:
Feeding America: 620 cents Barry Cunningham: 500 cents Feeding America: 1342 cents Total: 2462 cents
Two things to notice. addgift never says give — it does its work by growing the list it was handed (lists are shared: a change made through one name is seen through every name), and hands nothing back. And total walks the same list the Lesson 2 thread walked — but now the walk has a name, and the book will never have to write it again.
Your turn
Make a tool called firstandlast. It is handed a list and gives back its first and last items joined with " and ". Use it on ["sun", "moon", "stars"].
The worked answer — try yours first, then peek
The book says it answers:
sun and stars
You can now…
- build a function with
make, hand it values through
parameters, and hand an answer back with give;
- put loops and lists inside functions, each call with
fresh shelves of its own;
- grow a personal library of tools and build programs
out of them;
- add to a shared list from inside a function that
gives nothing back.
---