Variables have in programs some typical uses that are called roles of variables. For example, a variable whose value is never changed after initialization is called a fixed value. Roles of variables must not be mixed up with the basic types of variables (e.g., in Pascal Integer, Char, Boolean, etc.) since the concept of roles is a classification telling what kind of a task a variable has in a certain program.
The following ten roles are enough to cover almost all variables in simple programs. Roles appearing the most often are fixed value, stepper and most-recent holder which cover about 70 % of all variables. On the other hand, all variables don't necessary have any of the roles listed below.
The roles of variables (presented more accurately below) are:
Fixed value |
The role of a variable is a fixed value, if its value is not changed in run-time after initialization. The example program asks user the radius of a circle and then prints out the area of that circle. Variable r is a fixed value. This variable gets its value (on line 7) during the run-time only once. The value never changes after that. Fixed value can be used in different places in a program - twice on line 8 in the example.
(*1*) program AreaOfCircle (input,output); (*2*) const PII = 3.14; (*3*) var r: real; (*4*) begin (*5*) writeln; (*6*) write ('Enter the radius of a circle: '); (*7*) readln (r); (*8*) writeln ('The area of the circle is ', PII * r * r) (*9*) end.
Stepper |
Stepper goes through a succession of values in some systematic way. Below is an example of a loop structure where the variable multiplier is used as a stepper. The example program outputs a multiplication table while the stepper goes through the values from one to ten.
(*1*) program MultiplicationTable (output); (*2*) var multiplier: integer; (*3*) begin (*4*) for multiplier := 1 to 10 do (*5*) writeln(multiplier, ' * 3 = ', multiplier*3) (*6*) end.A stepper can also be used, for example, for counting and traversing the indexes of an array.
Most-recent holder |
A variable is a most-recent holder, if its value is the latest gone through value of a certain group or simply the latest input value. The example program repeteadly (on line 6) asks the user for the input until the input is valid. In this program the variable s is a most-recent holder since it holds the latest input value at the time.
(*1*) program AreaOfSquare (input,output); (*2*) var s: real; (*3*) begin (*4*) repeat (*5*) write ('Enter the length of the side of a square: '); (*6*) readln (s) (*7*) until s > 0; (*8*) writeln('The area of the square is ', s * s) (*9*) end.
Most-wanted holder |
The value of a most-wanted holder is the "best" or otherwise the most-wanted value out of the values gone through so far. The exists no restrictions for measuring the superiority of the values: the most-wanted can mean, for example, the smallest or the biggest number or a number closest to a certain value.
The example program finds out which is the smallest of the ten integers given as an input. The variable smallest is a most-wanted holder since it is given (on line 8) the most recent value if it is smaller than the smallest one so far.
(*1*) program SearchSmallest (input,output); (*2*) var i, smallest, number: integer; (*3*) begin (*4*) write('Enter the 1. number: '); readln(smallest); (*5*) for i := 2 to 10 do begin (*6*) write('Enter the ',i,'. number: '); (*7*) readln(number); (*8*) if number < smallest then smallest := number (*9*) end; (*10*) writeln ('The smallest number was ', smallest) (*11*) end.
(The variable i is a stepper and number is a most-recent holder.)
Gatherer |
The value of a gatherer accumulates all the values gone through so far. The example program accepts integers given as an input one by one until the user inputs the number -999 after which the program calculates the mean value of the inputs. The variable sum is a gatherer: the total of the inputs is gathered (on line 10) in it.
(*1*) program MeanValue (input,output); (*2*) var count: integer; (*3*) sum, number: real; (*4*) begin (*5*) sum := 0; (*6*) count := 0; (*7*) repeat (*8*) write('Enter a number, -999 to quit: '); (*9*) readln(number); (*10*) if number <> -999 then sum := sum+number; (*11*) if number <> -999 then count := count+1 (*12*) until number = -999; (*13*) if count > 0 then writeln('The mean value is ', sum / count) (*14*) end.
(The variable count is a stepper and number is a most-recent holder.)
Transformation |
A transformation is a variable that gets its new value always with the same calculation from value(s) of other variable(s). The example program asks the user for the capital and calculates both the interest and the total capital for ten years. The variable interest is a transformation and it is calculated (on line 8) using the capital.
(*1*) program Growth (input, output); (*2*) var capital, (*3*) interest: real; (*4*) i : integer; (*5*) begin (*6*) write('Enter capital (positive or negative): '); readln(capital); (*7*) for i := 1 to 10 do begin (*8*) interest := 0.05 * capital; (*9*) capital := capital + interest; (*10*) writeln('After ', i : 2, 'years: interest is ', interest : 11 : 2, (*11*) ' and total capital is ', capital : 12 : 2) (*12*) end (*13*) end.
(The variable capital is a gatherer and i is a counter.)
A transformation can be used for computing some result based on other items or coding the kind or type of an item.
One-way flag |
A one-way flag is a Boolean variable which once changed cannot get its original value anymore. The example program outputs the sum of input numbers given by the user and tells if there were any negative numbers among the inputs. The one-way flag neg watches (on line 10) whether there appears any negative numbers among the inputs and if at least one negative value is found, the variable will never return to value false.
(*1*) program SumTotal (input,output); (*2*) var number, sum: integer; (*3*) neg: Boolean; (*4*) begin (*5*) sum := 0; (*6*) neg := false; (*7*) repeat (*8*) write('Enter a number, 0 to quit: '); readln(number); (*9*) sum := sum + number; (*10*) if number < 0 then neg := true (*11*) until number = 0; (*12*) writeln('The sum is ', sum); (*13*) if neg then writeln('There was at least one negative number in inputs.') (*14*) end.
(The variable number is a most-recent holder and sum is a gatherer.)
A one-way flag can also be used, for example, to watch errors occuring in input data so that the program could ask the inputs again.
Follower |
A follower always gets the old value of another known variable as its new value. The example program asks the user for twelve integers and finally tells the biggest difference between the two successive input integers. The variable previous is a follower: it follows the variable current (on line 8).
(*1*) program BiggestDifference (input,output); (*2*) var month, current, previous, biggestDiff: integer; (*3*) begin (*4*) write('Enter the 1. value: '); readln(previous); (*5*) write('Enter the 2. value: '); readln(current); (*6*) biggestDiff := current - previous; (*7*) for month := 3 to 12 do begin (*8*) previous := current; (*9*) write('Enter the ', month, '. value: '); (*10*) readln(current); (*11*) if current - previous > biggestDiff (*12*) then biggestDiff := current - previous (*13*) end; (*14*) writeln('The biggest difference was ', biggestDiff) (*15*) end.
(The variable month is a stepper, current is a most-recent holder and biggestDiff is a most-wanted holder.)
Followers are used a lot with linked data structures to point the data element previous to the one in process.
Temporary |
A variable is a temporary if its value is always needed only for a very short period. The example program outputs first the bigger number and then the smaller number out of the two numbers given as inputs. The values of the variables are changed with each other by using a temporary variable tmp (on lines 7-9), whose value is later meaningless (no matter how long the program would run).
(*1*) program Swap (input, output); (*2*) var number1, number2, tmp: integer; (*3*) begin (*4*) write('Enter a number: '); readln(number1); (*5*) write('Enter another number: '); readln(number2); (*6*) if number1 < number2 then begin (*7*) tmp := number1; (*8*) number1 := number2; (*9*) number2 := tmp (*10*) end; (*11*) writeln('The bigger of the numbers is ', number1, (*12*) ' and the smaller is ', number2) (*13*) end.
A temporary is often used for changing the places of two data elements of an organizer.
Organizer |
An organizer is an array which is used for reorganizing its elements after initialization. The example program asks the user ten characters one by one, assigns them into the organizer word, reverses the order of the characters and finally outputs the characters in this reversed order.
(*1*) program Reverse (input, output); (*2*) var word: array[1..10] of char; (*3*) tmp: char; (*4*) i: integer; (*5*) begin (*6*) for i := 1 to 10 do begin (*7*) write('Enter a letter: '); readln(word[i]) (*8*) end; (*9*) for i := 1 to 5 do begin (*10*) tmp := word[i]; (*11*) word[i] := word[11-i]; (*12*) word[11-i] := tmp (*13*) end; (*14*) for i:=1 to 10 do write(word[i]); (*15*) writeln (*16*) end.
(The variable tmp is a temporary and i is a stepper.)
An organizer can be used for sorting an array or for someother reorganization.
Last update: 21.4.2004