A Bunch of Questions About Variables!

This post is made in the hopes that some answers might be found for the following questions related to variables as a concept and in practice. This is by no means an exhaustive list of questions, but hopefully they generate discussion. A number of these questions may come across as pedantic, especially given the “black box” methodology where we don’t need to know how things work. Others might display a lack of fundamental understandings. By no means to I expect any one person to have all of these answers (although if you do, nice!), but I would encourage anyone willing to approach them to do so with the goal in mind of promoting and rewarding curiosity for new learners by entertaining the questions, even if they seem foolish, for those who want to know.

  1. According to Techopedia.com, “[v]ariables generally have four attributes: an identifier, data location, type and value.” How might one define each of these four attributes, and why does it matter that variables possess these attributes?

  2. Variables stand in place of data, but “data” itself is a somewhat vague term, so what can “data” be? In other words, what sort of data is any given variable able to hold, and what is it unable to hold?

For example, it might seem obvious that the variable ‘x’ could be assigned to something like an integer value of 5, but could ‘x’ hold more than numeric values?

Could we say things like:
x = “Hey, Bob”, where x is meant to hold characters (or possibly alphanumeric values)
x = draw(), where ‘x’ is meant to hold an entire function (or possibly a function’s value)
x= Bird, where ‘x’ is meant to hold the class ‘Bird’ (or possibly the value of a class)
and so on?

If so, how and when might it become useful to use variables to hold different types of data?

  1. Is there a limit to the amount of things that a variable can be assigned to? If it is possible to assign more than one thing to the same variable, when might it be appropriate to do so?

By extension, is there a limit to how much memory space a variable can take up once something is assigned to it? If possible, what might make a variable take up more space?

  1. The idea of “built-in variables” is one that comes up almost immediately when learning in Processing, and it might be similar in other languages. What exactly is the difference between “built-in” variables and regular variables? Is it possible for a user to build their own variables into Processing, and if so, how and when might it be useful to do so?

  2. Knowing that variables can exist as “built-in” features of a programming language, can variables also exist in other ways? One might come across terms like “local variable,” “global variable,” and so on, which seems to suggest that variables can have different types/states/ways-of-existing, so what might those be? What do these different sorts of variables mean in the broader context of programming?

i think that academic discussion about coding ( and teaching/learning coding )
is very welcome here,
but as you posted under

Processing / Beginners

i would like to start also on that beginner level,
and in the way what is most liked here:

teach by examples

/*
-A- built in ( examples )
 mathematical constants      PI
 
 system variables            CENTER
 
 variables                   mouseX
 
-B- user static settings ( not variable )
 final float my_diameter = 30;
 
-C- user variables
 float my_circumference;

-D- user functions ( or class.. )
 void my_calc() {}
 
 */
// test code                               ( using processing 3.5.3 IDE on a win7 / 64bit )

//                                         create global user variables
final float my_diameter = 50;           // static 
float my_circumference;                 // variable
boolean diagp = true;                   // false // diagnostic print

void setup() {
  size(200, 200);
  my_settings();
  my_circumference = my_circle(my_diameter);
}

void draw() {
  // background(200,200,0);              // disabled for PAINT MODE
  circle(mouseX, mouseY, my_diameter);
}

void my_settings() {
  background(200, 200, 0);               // yellow
  stroke(0, 200, 0);                     // green
  strokeWeight(3);                       // border
  fill(0, 0, 200);                       // blue  
  ellipseMode(CENTER);                   // anyhow default
}

float my_circle(float diameter) {        // user function
  float circumference = PI * diameter;   // local variable
  if ( diagp ) println("diameter: "+ diameter+" circumference: "+ circumference );
  return circumference;                  // to be used in MAIN
}


Processing has an original Java mode, but there is also p5.js, processing.py, Processing.R, and JRubyArt. The questions you are asking have abstract answers, but some specific answers relate to the type system of the specific language.

For example, it might seem obvious that the variable ‘x’ could be assigned to something like an integer value of 5, but could ‘x’ hold more than numeric values?

For example, this is meaningful in Python:

x = 5
x = 'five'

But in Java, no. Instead:

int xint = 5;
// xint = "five";  // illegal
String xstr = "five";
1 Like

This sounds oddly like a homework question.

It is also the kind of thing that tends to be taught by example in tutorials. For example, the tutorial you reference already addresses this with an example –

For example, the syntax int temp=0 ; in JAVA or C++, “temp” is the variable name; the type of variable is integer and its value is 0.

1 Like

Just a few remarks

You can draw an ellipse at position 45,155 without using a variable.

But when you think of it like a ball that is supposed to fly, you get into trouble. You can’t increase 45,155.

Same is true when you want to get the player name for the high score in a game. You can’t put all names like Tim and Anna in the sketch.

Here variables come into play.

Think of a variable as a box with a name on it (x_position) and a content in it (45). Now you can just add something to x_position and the ball flies. You are Independent of the 45 now and can start with a random value instead and just let the ball fly.

fill(255,0,0); 
ellipse(x_position, y_position, 6,6); 
x_position = x_position + 6; 
y_position = y_position + 4;

Similar with the player name.

Of course variables and functions are very common in math, and programming stems from math in a way.

Type

Now in processing/Java the variables additionally have a explicit type which is a good thing. It’s a bit strict but makes the purpose of the variable clearer. It also prevents certain programming errors. A type can be int, float and String etc.

The type tells which values are allowed to get into the box!

Classes and objects

By the way when you think of String or PVector (see reference for both: https://www.processing.org/reference/) these are not only variable types but classes which contain multiple variables and even functions. You can think of them as complex or extended kind of variable types. You can also define your own classes hence your own variable types (e.g. Person with name, address and age).

I simplify things here a bit but you can read the tutorial about objects.

https://www.processing.org/tutorials/objects/

Lists

There are also arrays, ArrayList and the like being lists of variables or objects. These are useful when you want to have a lot of balls or persons: you can for-loop over thousands of them with just a few lines of code.

Chrisir