# Placement of variables

**URL:** <https://discourse.processing.org/t/placement-of-variables/1875>\
**Category:** Coding Questions\
**Created:** [July 18, 2018, 8:32am UTC](https://discourse.processing.org/t/placement-of-variables/1875 "2018-07-18T08:32:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattwonderwall](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mattwonderwall](https://discourse.processing.org/u/mattwonderwall)\
**Post date:** [July 18, 2018, 8:32am UTC](https://discourse.processing.org/t/placement-of-variables/1875/1 "2018-07-18T08:32:13Z")

</div>

I was following a course book on processing which wanted me to recreate an image: it wanted me to center a circle in each quarter of a square. It then wanted me to replace all the hard-coded numbers with variables. When I did so before the code, my image became four circles packed into the top left quarter of the square. But when i put it in void draw, it did it fine.

Why did this happen?

```auto
int ellipseSize = 50;
int bckgrndClr = 255;
int strokeClr = 0;
int fillClr = 190;
  
void setup(){
  size(320,320);
}

void draw(){
//these are the variables which the question are about
  float quartHeight = (height/4);
  float halfHeight = (height/2);
  float quartWidth = (width/4);
  float halfWidth = (width/2);

  background(bckgrndClr);
  stroke(strokeClr);
  fill(fillClr);
  ellipse(quartHeight, quartWidth,ellipseSize,ellipseSize);
  ellipse(halfHeight+quartHeight, halfWidth+quartWidth,ellipseSize,ellipseSize);
  ellipse(quartHeight, halfHeight+quartWidth,ellipseSize,ellipseSize);
  ellipse(halfHeight+quartHeight, quartWidth,ellipseSize,ellipseSize);
  
 ellipseSize=ellipseSize+1;
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 18, 2018, 8:38am UTC](https://discourse.processing.org/t/placement-of-variables/1875/2 "2018-07-18T08:38:40Z")

</div>

At the global scope - outside of any function - you shouldn’t assign variables values based on the values stored in `width` and `height`. These values are not properly set up until after the call to `size()` happens in `setup()`!

You should declare your values globally, and then assign them a value in `setup()` (after the call to `size()`):

```auto
int half_width;

void setup(){
  size( 600, 400 );
  half_width = width / 2; // 300;
}

// ...

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 18, 2018, 8:40am UTC](https://discourse.processing.org/t/placement-of-variables/1875/3 "2018-07-18T08:40:50Z")

</div>

- [https://Forum.Processing.org/two/discussion/27966/conventions-re-scope-where-to-initialize-variables#Item\_3](https://Forum.Processing.org/two/discussion/27966/conventions-re-scope-where-to-initialize-variables#Item_3)

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [July 18, 2018, 2:04pm UTC](https://discourse.processing.org/t/placement-of-variables/1875/4 "2018-07-18T14:04:07Z")

</div>

This is a common issue for ppl starting with processing. I am making a mental note… it should be added [here](https://github.com/processing/processing/wiki/Common-Errors)

Kf
