# Day 4: Knots and Functions

2022-06-10. Filed in public, twoville.

Welcome to day 4 of our fabrication summer camp. Let's look back on the week and consider the big computational ideas we've seen:

• When we write code, we are teaching the machine to do something.
• Programs are not just code. They are also data, and that data is often numbers.
• Data can be named. Variables make the code easier to understand and modify.
• We can do the same thing over and over again using loops.
• We programmers make a lot of mistakes. But we make fewer with practice.

Today we reach our last big idea: code too can be named. We'll explore this idea as we trace out knots. A knot is a path that meanders around, sometimes in interesting and satisfying ways, and loops back up with itself.

## Naming Code

If there's a sequence of code that we find ourselves writing again and again, we can name it. A named sequence of code is called a function. To motivate functions, we need to find a shape with some repetition, like this endless knot:

Imagine a turtle starting at the bottom and looking northeast. It follows these steps:

1. move forward one unit
2. turn left
3. move forward four units
4. turn right
5. move forward one unit
6. turn right
7. move forward four units
8. turn left
9. move forward one unit
10. turn left
11. move forward four units
12. turn right

That's only the first half of the steps, but that's enough to show the repetition. There are two basic actions that make up this knot: trace out an L-shape by turning left and trace out an L-shape by turning right. We'll need two functions for the two actions. But first, we set up the shape:

Next we define the functions inside the path:

Now we have all the vocabulary we need to trace out the path, which has some beautiful symmetry:

### Practice Exercise

In the turtle geometry designs we've made so far, the turns have been very sharp. The world could use a few more round edges, wouldn't you say? There's a command to make them: curl. You use it instead of turn. For example, to make a chain link, we'd walk forward and then curl 180 degrees back around. That makes half the link. If we do it twice, we get a complete link:

Try out curl and functions by recreating this Bowen knot:

This knot looks a lot like the icon on the Command key on Macintosh computer.

## Parameters

When we name code, it becomes reusable. We can call it again and again and again, and it does exactly the same thing every time. But maybe we don't want it to do exactly the same thing. Consider navigating a turtle along a paperclip. It follows these similar but not identical steps.

1. Walk forward.
2. Curl around 180 degrees.
3. Walk forward more than before.
4. Curl around in a semi-circle bigger than before.
5. Walk forward even more.
6. Curl around in an even bigger semi-circle.
7. Walk forward about the same amount as last time.

Steps 1-2, 3-4, and 5-6 are very much a like, but they differ in number. We can still put them in a reusable function, but we must leave the distance walked and the curl radius open. We treat them like variables instead of fixed numbers. We list these variables between the parentheses of the function's header. Variables that must be set when a function is called are parameters. Every time we call the function, we can pass different numbers in to these parameters. Here's our paperclip:

### Design Exercise

Your task for the remainder of the day is to wind a path around in an interesting manner using functions. Piece your path together by placing instances of these tiles on grid paper:

Rotate them as you see fit. Form a closed loop. Measure the walk distances and the degrees of each curl. Show your measured designed to a teacher.

Trace out your path in Twoville. If you have any motions that are repeated, use a function with parameters. We will help you embroider your path on the front of a greeting card.