Novice coder needing help - cannot be resolved to a variable error

I am a beginner and finding a problem. I wrote the following code and had error message “a cannot be resolved to a variable”. The rectangle appears fine with numbers but will not accept a declared integer.

void setup () {
  size (500,400);
  int a = 100;
} 
 
void draw() {
  rect(a, 100, 200, 200);
}

Any help appreciated,
JRK

1 Like

The problem here is that since you’ve declared a inside the setup() function, it’s only available inside the setup() function. If you want variables to be available inside multiple functions, you have to declare them outside the functions. Something like this:

int a;

void setup () {
  size (500,400);
  a = 100;
}

void draw(){
  rect(a, 100, 200, 200);
}

Here is a bit more info about scope.

4 Likes

Many thanks. That works much better. Despite having the beginners’ book “Getting Started with Processing” I guess I’ll be using this forum a lot.
JRK

2 Likes