# Day 4: Knots and Functions

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:

- move forward one unit
- turn left
- move forward four units
- turn right
- move forward one unit
- turn right
- move forward four units
- turn left
- move forward one unit
- turn left
- move forward four units
- 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.

- Walk forward.
- Curl around 180 degrees.
- Walk forward more than before.
- Curl around in a semi-circle bigger than before.
- Walk forward even more.
- Curl around in an even bigger semi-circle.
- 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.