Variables [7/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 the 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.
Exercise
1. Change the name of variable
x to i.
[Run the program]
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.
[Run the program]
A println statement without any arguments has the effect of advancing the text output to a new line.
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.
[Run the program]
Exercises:
1. 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.
Solution
c = 5/2;
2. Print out the value of variable c.
Solution
println c;
3. Introduce a new variable named oneThird at the end of the source code, and set its value to result of expression 1/3.
Solution
#num oneThird = 1/3;
4. 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, because it needs an infinite number of digits. Type num uses the decimal number system to store values.
This program draws three rows of discs.
The disc size is 90 pixels.
[Run the program]
Exercises:
1. Change the initial value of the variable size in the first statement. Try several values in the range from 30 to 200.
2. Set the initial value of the variable size to value 35.
[Run the program]
3. Modify the literals (i.e. literal expressions) 1, 2 and 3 in the disc procedure calls. Try some other small numbers, and observe the effects.
A call of the function rn produces a random whole number 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.
A function call is similar to a procedure call, except that a function call produces a value. In this program, function rn is called two times. The function rn accepts one argument.
A function call can be easily recognized by parenthesis () that must follow the function name. Arguments of a function call are listed inside parenthesis.
Exercise
1. Modify the program such that a third random number in the range from 1 to 33 also gets printed out.
This program prints 12 random numbers in the range from 1 to 10.
[Run the program]
Exercise:
(Very challenging!) Modify this program so that it prints out 16 random numbers in the range from 100 to 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.
[Run the program]
This program calls the function rn, twice.
This program calls two procedures: circle and sleepMs.
The difference between functions and procedures is that functions produce a value, but they don't otherwise change anything by themselves. In contrast, procedures are allowed to produce various changes, like changing the output on the display. For example, the circle procedure modifies the output by drawing a circle.
To make functions and procedures more confusing, procedures are also usually called “functions”.
The first two arguments of procedure circle are coordinates of circle's center. The third argument is circle's radius.
Exercises:
Insert a statement
print x, " ", y, " || ";
just above the call of procedure circle.
[Run the program]
2. Change the inserted print statement
into a println statement.
[Run the program]
3. Change the inserted println statement into: println [y:30, x, " ", y, " "]; [Run the program]
4. 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.
[Run the program]
Exercises:
1. Move the first statement of the program
inside the for loop. Make it a first statement
of the loop.
[Run the program]
2. Add a println statement just above the sleepMs statement that prints out the coordinate x.
Solution
println x;
[Run the program]
3. Modify the println statement such that prints out both the coordinates x and y, separated by a space.
Solution
println x, " ", y;
[Run the program]
4. Modify the println statement so that it prints out the coordinates in the form (x, y) - for example, like: (321, 456).
Solution
println "(", x, ", ", y, ")";
[Run the program]
5. 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.
This program draws many small discs of small radius.
The radius is a random value in the range from 2 to 7 .
[Run the program]
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 it. More generaly, the scope of any variable is restricted to the block in which the variable is introduced.
Exercises:
1. Make the program print out the value of variable
radius by adding
a println statement at the end of the source code.
Also, print a string
"The radius is:"
before the value of variable
radius. Run and restart the program several times.
2. Un-comment the println statement below the comment "Error 1", to see the error message.
Put the line in the comment again (comment it out).
3. Un-comment the println statement below the comment "Error 2", to see the error message.
Put the line in the comment again (comment it out).
Names of variables must begin with a letter or an underscore (_) symbol.
Other characters of a variable name must be letters, underscores or digits.
[Run the program]
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.
Exercises 1-3:
Un-comment the erroneous lines one by one and correct the invalid variable names.
[Run the program]
Both println statements print out the value of variable
x.
[Run the program]
Exercise
Names of named arguments are separate from variable names.
1. Change the name of variable
x to fg. Change it in all three places.
[Run the program]
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.
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 variable vx should be set to zero. The variable vy 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; << vx = -460 / 100 #num vy = -3; << vy = -300 / 100
This demonstrational program contains an error.
Correct the error and run the program.
Solution (click):
A semicolon (;) is missing.
#num radius=40;
Colors are explained in the next chapter.
Exercise:
Change the initial value of the variable radius to some other value in the range from 10 to 100. Try several different values.
<< F2:Prev - - F4:Next >>