Variables [1/14]
Variables
A variable is a name that can be associated to a value.
A variable must be introduced before it is used.
Variables are introduced by a statement of new variables. A statement of new variables begins with the symbol #.
Each variable must be of some type. In this example, the variable x is of type num. In other words, the first statement introduces a variable named x of type num, with initial value of 10.
The type num is used for numbers. Therefore, the variable x cannot be assigned anything but a number. For example, a string cannot be assigned to x.
The value of a variable can be changed. In this example, the variable x was equal to 10 at the start, but later its value was changed to 3.
Two variables cannot have identical names.
Click on the Check button to see the error description.
Correct the example by changing the name of the second variable, and then output its value to the screen.
Variable names and other names are case sensitive. Therefore, 'a' and 'A' are two different names.
* * *
-- Assignment vs Intoducing Variables --
Another way to correct this error would be to just use an assignment instead of introducing a new variable. A new variable is introduced by a statement of new variables, for example:
#num a=5;
An assignment is performed by a statement of assignment. Notice the difference:
a=5;
A variable name is a primary expression, therefore it can be used as a part of a complex expression.
A value of a variable can be changed. In this program, the value of variable c is changed by the statement of assignment in the next-to-last statement of this program.
After running the program: At the end of the source code, add a statement that changes value of the variable c once again, this time to expression 5/2. Then print out the value of variable c.
After that, introduce a variable named oneThird at the end of the source code, and set it's value to result of expression 1/3. Then print out the value of variable oneThird.
Observe that the printed value is only approximately correct. As computers are finite machines, they cannot store the exact result of expression 1/3 as a decimal number. Type num uses decimal system to store values.
This program draws three rows of discs. The disc size is 90 pixels.
Try changing the initial value of the variable size in the first statement to set a different disc size.
After that, try modifying the literals (i.e. literal expressions) 1, 2 and 3 in the disc drawing statements. Try some other numbers, and observe the effects.
The function rn produces random whole numbers in the range from 1 to the value given by the argument.
In the first statement of the program the function rn is called with an argument 10.
In this example program, the function rn is called two times. The function rn accepts one argument.
A function call can be easily distinguished by parenthesis () following the function name. Arguments of a function call are listed inside parenthesis.
This program prints 12 random numbers in the range 1–10.
Exercise (a tricky one):
Modify this program so that it prints out 16 random numbers in the range 100–110.
Solution
for #i=1..16 { #num x = rn(11)+99; println x; sleepMs(300); }
Almost no beginner can solve this exercise correctly. The point of the exercise is:
- to make the reader remember how this task is accomplished
- to make the reader aware that learning programming is not easy, even when it seems easy
Some things seem easy, but they are not. Some things are easy only after the solution is known.
This program draws 40 circles at random positions on the screen.
This program calls three functions: rn, circle and sleepMs.
The first two arguments of function circle are coordinates of circle's center. The third argument is circle's radius.
After running the program, insert a statement print x, " ", y, " || "; just above the call of function circle. Run the program.
After that, change the inserted print statement into a println statement.
After running the program, change the inserted println statement into: println [y:30, x, " ", y, " "];
After running the program, try changing the arguments of rn function calls to some smaller values and observe the effects. Try a few different variations in both calls of the rn function.
This program chooses a random point. Then it draws 22 circles around it with a random radius of up to 190.
After running the program, try moving the first statement of the program inside the for loop. Make it a first statement of the loop. Run the program.
After that, add a println statement just above the sleepMs statement that prints out the coordinates x and y. Then run the program.
After that, modify the previously inserted statement so that it prints out the coordinates in the form (x, y) - for example, like: (321, 456).
Solution
println "(", x, ", ", y, ")";
After that, make the program draw 50 circles instead of 22.
This program contains an error.
Correct the error and run the program.
Solution (click)
The variable radius must be introduced before it is used.
#num radius = rn(200)+30;
Exercise:
Paint the circles in various colors.
All variables introduced in a program have a certain scope. The scope of a variable defines parts of the program where the variable can be used.
For example, the scope of variable i in this program is restricted to the for loop. Variable i is not available outside the for loop.
The variable radius is available everywhere below the place where it was introduced.
The variable x is available only in its block, not outside. More generaly, the scope of any variable is restricted to the block in which the variable is introduced.
After running and examining the program, uncomment the errorneous lines (in this case, the lines containing the println statement) to see the error message. Then make those erroneous lines become comments again..
After that, make the program print out the value of variable radius after the for loop. Also, printout a string The radius is: before the value of variable radius. Then run the program several times.
Variable names must begin with a letter or an underscore (_) symbol. Other characters of a variable name must be letters, underscores or digits.
A variable name cannot begin with a digit, but it can contain digits. A variable name cannot contain blanks, it must be a single word. Also, it cannot contain a minus (-) symbol.
After running and analyzing the program, uncomment the erroneous lines one by one and correct the invalid variable names.
Run the program to verify that it is correct. Of course, the additional variables will not be printed out without appropriate println statements.
Both println statements print out the value of variable x.
Names of named arguments are separate from variable names.
Can you change the name of variable x to fg? Change it in all three places. Then try it out.
In this example, the purple disc moves across the screen. The disc starts the motion from the coordinates (460, 300). Run the program.
The disc's motion vector is (+5, -3); it is given by variables vx and vy. The numbers +5 and -3 are called vector's components.
Vectors are explained in great detail in one of the future chapters. For the present time, it doesn't matter if you don't know what vectors are.
After running the program, try changing the motion vector by modifying the inital values of variables vx and vy so that the disc moves in some other direction and with different speed. Try out several combinations of values in the range from -10 to 10.
Vectors are commonly used to set speed and direction.
Exercise 1:
Change the motion vector so that the disc moves downward.
Solution
The vx variable should be set to zero. The vy variable should be set to a positive number.
Exercise 2:
Change the motion vector so that the disc ends the motion at the top left corner of the screen.
Solution
#num vx = -4.6; #num vy = -3;
This demonstrational program contains an error.
Correct the error and run the program.
Solution (click):
A semicolon (;) is missing.
#num radius=40;
Try changing the initial value of variable radius afterwards.
Colors are explained in the next chapter. Therefore, click on the Next > button to proceed.
<< F2:Prev - - F4:Next >>