CPSC 110-08: Computing on Mobile Phones
Spring 2012

Homework: Variables and Values

CS Principles

This activity introduces the concept of abstraction. It focuses, in part, on the following learning objective:

Variables and Abstraction

In the Paint Pot tutorial you defined three global variables to control the size of the dots that are drawn on the screen, big, small, and dotsize. As you see, using a variable in this app helped make the app much more functional -- i.e., the user could change the size of the dots that are drawn without your having to change the program's blocks.

This is an important example of abstraction at work -- in this case, we are letting an abstract symbol (the variable dotsize) represent or stand for something else (its value, 2 or 8). Do you see how this statement


set r to dotsize

is more abstract (more general) than this statement:

set r to 8

The second statement can only set the radius, r, to 8, whereas the first statement can set it to any value stored in the variable dotsize.

What is a Variable?

A variable in a computer program is a symbol that represents a memory location where a piece of data can be stored. You can think of a computer memory as a large array of numbered mail boxes, where the numbers represent the address of the memory location. In this example, there are 8 memory locations numbered 17-24 (in decimal) and 10001 - 11000 (in binary). None of the locations have any value stored in them yet.

1718192021222324
1000110010100111010010101101101011111000
        

When you define a global variable in App Inventor, you are giving name to a memory location. For example, the following variable definitions would result in a memory that looks something like model shown here.

smallbigdotsize2021222324
1000110010100111010010101101101011111000
282     

What is a Value

In a computer program a value is a symbol of a piece of data. For example, the numeral '8' represents the number 8. Values (data) are stored in the computer's memory locations.

Symbols like the numeral '8' are also examples of abstractions. The numeral '8' and the word 'eight' and the binary string '1000' are all symbols that represent the number 8, which is itself an abstract concept of 8 things. For example, they can all be used to refer to the number of little circles here: o o o o o o o o.

So, in a computer program we use abstract symbols to represent both variables and values.

Values vs. Variables

It's important to distinguish between the variable's name (e.g., big or small) from the value that it represents -- i.e, from the value that it is storing in its memory location (e.g., 8 or 2). For example, in the following blocks:

when you perform the set global dotsize to big operation in your app you are setting dotsize to big's value (8) not to its name (big). This is how App Inventor's memory would look after you do this:

smallbigdotsize2021222324
1000110010100111010010101101101011111000
288     

Self-Study Quiz

To make sure you understand this distinction between a variable and its value, try testing yourself on the following interacive quizzes.