> For the complete documentation index, see [llms.txt](https://vyuka.digitalneumenia.sk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vyuka.digitalneumenia.sk/creative-programming-i/02c-variables-our-own-variables.md).

# 02c - Variables: Our own variables

So far we used variables created and maintained by computer: `width`, `height`, `mouseX`, `mouseY`. The computer knows the size of the screen it puts the width of the screen into the box with label "`width`". It knows where the mouse is and it puts the horizontal position of mouse into the box with label "`mouseX`".

We can also create our own memory boxes and store numbers in them. Let's start with an example. Draw a circle of size 120 at the position of mouse:

<figure><img src="https://4146983298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMUDTJmsKX4zkjR9asQK3%2Fuploads%2FLuT6MPwc5pIh9DJx3FnH%2Fimage.png?alt=media&amp;token=241555e3-bf13-407b-ac4d-7813361f03c4" alt=""><figcaption></figcaption></figure>

You should be able to write this code on your own now. But just in case, here it is:

<pre><code>function setup() {
  createCanvas(windowWidth, windowHeight);
  stroke(0,255,0);
  noFill();  
}

function draw() {  
  background(0);  
<strong>  circle(mouseX, mouseY, 120);
</strong>}
</code></pre>

Now we want to draw a square that is the same size as the circle and also centered around mouse.

If we put the square at the same position, the square is drawn not from center but from its top-left corner:

<figure><img src="https://4146983298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMUDTJmsKX4zkjR9asQK3%2Fuploads%2FUIAxXxunsE1VoWCKmtjp%2Fimage.png?alt=media&amp;token=1e832700-d2f9-4cd8-9963-1f258d9c151d" alt=""><figcaption></figcaption></figure>

<pre><code>function setup() {
  createCanvas(windowWidth, windowHeight);
  stroke(0,255,0);
  noFill();  
}

function draw() {  
  background(0);  
  circle(mouseX, mouseY, 120);
<strong>  square(mouseX, mouseY, 120);
</strong>}

</code></pre>

Since square is not drawn from its center but from its top-left corner, we need to move the square position left by half of its size, and upward by half of its size:

```
  square(mouseX - 120/2, mouseY - 120/2, 120);
```

After this change, the square is nicely aligned with the circle:

<figure><img src="https://4146983298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMUDTJmsKX4zkjR9asQK3%2Fuploads%2FyZ1AaqvXVFyeg4zk4pqH%2Fimage.png?alt=media&amp;token=55e3e5b2-3ad7-457a-84ff-d33a7ad24fa7" alt=""><figcaption></figcaption></figure>

## When things change

What if we wanted to change the size of this symbol from 120 to 40. We would need to change all the numbers in our code, 120 to 40:

<pre><code>function setup() {
  createCanvas(windowWidth, windowHeight);
  stroke(0,255,0);
  noFill();  
}

function draw() {  
  background(0);  
<strong>  circle(mouseX, mouseY, 40);
</strong><strong>  square(mouseX - 40/2, mouseY - 40/2, 40);
</strong>}
</code></pre>

<figure><img src="https://4146983298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMUDTJmsKX4zkjR9asQK3%2Fuploads%2FT02NLJcgrHuJRG7hVzik%2Fimage.png?alt=media&amp;token=256c5988-3866-4c6f-a02f-2700716b724a" alt=""><figcaption></figcaption></figure>

Imagine there are dozens of various objects, all relating to the size of the circle. We would have to change tens of various numbers in our code.

## Let there be variable

Just like we used `width` and `height` to save us from rewriting many numbers in our code, we can now use a variable that will store the size of the circle. Unlike `width` or `mouseX`, computer doesn't know how that variable is labeled and what's inside its box. We must do it on our own.

Let's call or variable `size` because it will represent the size of our circle. And let's store the value `40` inside the variable. This is how it's done:

<pre><code><strong>let size = 40;
</strong>
function setup() {
  createCanvas(windowWidth, windowHeight);
  stroke(0,255,0);
  noFill();  
}

function draw() {  
  background(0);  
  circle(mouseX, mouseY, 40);
  square(mouseX - 40/2, mouseY - 40/2, 40);
}

</code></pre>

{% hint style="info" %}
You can name your variable whatever you like, e.g. `velkost` or `diameter` or `little_guy`. Just don't start the variable name with number, e.g. `3corners` or use spaces in the name, e.g. `circle size`.&#x20;
{% endhint %}

And now we can use this variable instead of the number in our code:

<pre><code>let size = 40;

function setup() {
  createCanvas(windowWidth, windowHeight);
  stroke(0,255,0);
  noFill();  
}

function draw() {  
  background(0);  
<strong>  circle(mouseX, mouseY, size);
</strong><strong>  square(mouseX - size/2, mouseY - size/2, size);
</strong>}
</code></pre>

You might notice that our variable has a different color in editor than `mouseX` or `windowHeight`. That's because `mouseX` and `windowHeight` are special variables; created and maintained by computer. We can use them without creating them with `let` and without storing number in them with `=`.

## Why do we use variables?

Now we can change the value of our variable `size` from 40 to any number, e.g. 194 and we don't need to change anything else in the code.

<pre><code><strong>let size = 196;
</strong>
function setup() {
  createCanvas(windowWidth, windowHeight);
  stroke(0,255,0);
  noFill();  
}

function draw() {  
  background(0);  
  circle(mouseX, mouseY, size);
  square(mouseX - size/2, mouseY - size/2, size);
  
}
</code></pre>

&#x20;The sketch works flawlessly. Both circle and square adapt to the new value of `size` without us having to change all the number that these commands contained before.

&#x20;![](https://4146983298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMUDTJmsKX4zkjR9asQK3%2Fuploads%2Fl6rEpNX1o2xJ2OWSlDPc%2Fimage.png?alt=media\&token=387e038f-e3e7-47a9-b49d-866f3d7863a5)

## Best practice

If a number is repeated multiple times in your, such as the size of the circle (`40`) was used in the position of the square and the size of the square, it's good practice to replace the multiple occurrences of this number with a variable. Just like we did.

It will help you in future to write code that takes less effort to change, update or maintain. You only need to change the value of the one variable and the rest of the code works automatically.&#x20;
