1. Giving the Turtle a Memory: Understanding Variables
- Giving the Turtle a Memory: Understanding Variables
At its core, a variable is simply a named storage location—like a labeled box—where you can keep a piece of information to use later. You can store numbers for drawing, like a side length, or even words to be printed on the screen. The power of variables is that they make your programs flexible. Instead of typing the same value over and over again, you can store it in a variable. If you want to change that value, you only have to change it in one place, and your entire program will update automatically.
Syntax and Usage
In Logo, you primarily work with variables using two simple constructs: one for creating the variable and one for accessing its value.
| Command Syntax | What It Does |
| make “variable_name value | Creates a variable with a specific name and assigns it a value. E.g., make “side 100 or make “first_name Amal. |
| :variable_name | Accesses or retrieves the value stored inside the variable. E.g., print :side. |
Practical Example
Imagine drawing a square. Without variables, you have to type the side length four times. With a variable, you define the side length just once.
make “side_length 100
fd :side_length
rt 90
fd :side_length
rt 90
fd :side_length
rt 90
fd :side_length
rt 90
Key Insight: This is your first taste of making a program flexible. You are in control, and you can change the drawing with one tiny edit! To draw a bigger square, you don’t need to rewrite all the fd commands. You only need to change the very first line to make “side_length 150. This single change allows you to draw a square of any size.
While variables are great for reducing repetition of a single value, the next step is to package an entire sequence of commands into a brand new, single command that you can call anytime you want.