# Let/var behaviour inside function vs outside

**URL:** <https://discourse.processing.org/t/let-var-behaviour-inside-function-vs-outside/32639>\
**Category:** Beginners\
**Created:** [October 5, 2021, 4:31pm UTC](https://discourse.processing.org/t/let-var-behaviour-inside-function-vs-outside/32639 "2021-10-05T16:31:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADG](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@ADG](https://discourse.processing.org/u/ADG)\
**Post date:** [October 5, 2021, 4:31pm UTC](https://discourse.processing.org/t/let-var-behaviour-inside-function-vs-outside/32639/1 "2021-10-05T16:31:51Z")

</div>

Probably a dumb question, but in this example:

```auto
var angle = 0.0;
function setup() {
createCanvas(120, 120);
background(204);
}
function draw() {
translate(mouseX, mouseY);
rotate(angle);
rect(-15, -15, 30, 30);
angle += 0.1;
}

```

everything works fine, but as soon as I move the `var angle = 0.0;` inside `function draw()` the rectangle will not spin. Why is that?  
Why only global variable? Do built in functions/objects like rotate require global variables?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 5, 2021, 4:52pm UTC](https://discourse.processing.org/t/let-var-behaviour-inside-function-vs-outside/32639/2 "2021-10-05T16:52:54Z")

</div>

Hi @ADG

I advise you to look for variables scope.

When you declare you variable inside `draw()` it only exists inside of it.  
Also, keep in mind that draw is just a function that is called over and over during the execution of your program.  
So when the program arrive at the end of your first call of `draw()`, it doesn’t need the variables created in it anymore and get rid of them.  
During the second call, your variable angle is created again and thus, even if in your code it seems to be the same variable, it is not for the program.

---

<div class="post-metadata">

**Author:** ![ADG](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@ADG](https://discourse.processing.org/u/ADG)\
**Post date:** [October 5, 2021, 5:48pm UTC](https://discourse.processing.org/t/let-var-behaviour-inside-function-vs-outside/32639/3 "2021-10-05T17:48:34Z")

</div>

Thank you @jb4x.

I think I understand. So because there isn’t a global variable to hold the change it gets discarded at the end of the loop is that correct? I’m sure I’ll get a better understanding once I read into variable scope.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 5, 2021, 6:03pm UTC](https://discourse.processing.org/t/let-var-behaviour-inside-function-vs-outside/32639/4 "2021-10-05T18:03:41Z")

</div>

That’s it.

When you enter a function, some space in memory is allocated to the variables defined in that function. When you exit that function, the memory allocated is freed allowing other program to use that part of the memory.

So in your, case, what is happening is:

1. The `draw()` function is called
2. A variable called `angle` is created and set to 0
3. 0.1 is added to the variable `angle` that now holds 0.1
4. The end of the function `draw()` is reached
5. The memory is freed so `angle` does not exist anymore
6. The `draw()` function is called for an (n+1)th time
7. Go back to 2.

I hope it makes it clearer.
