Roles of Variables

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 eleven 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:

The meaning of line numbering in example programs is to ease refering to lines. Line numbers have no effect on program execution since they are comments in Pascal.

Icon for the role 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.

Icon for the role 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.

Icon for the role 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.

Icon for the role 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.)

Icon for the role 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.)

Icon for the role 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.

Icon for the role 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.

Icon for the role Temporary

A variable is a temporary if its value is always needed only for a very short period. The example program asks the user for tax-free prices and prints out the total price and the tax for each item. The tax is computed into the temporary tax (on line 8) which is immediately used twice in the next statement (on lines 9 and 10).

(* 1 *)  program Taxation(input,output);
(* 2 *)  const taxRate = 16;
(* 3 *)  var taxfree, tax: real;
(* 4 *)  begin
(* 5 *)     repeat
(* 6 *)     write('Enter tax-free price (0 to quit): '); readln(taxfree);
(* 7 *)        if taxfree <> 0 then begin
(* 8 *)           tax := taxfree * taxRate / 100;
(* 9 *)           writeln('Total price ', taxfree+tax : 1 : 2,
(* 10 *)                  ' that includes tax ', tax : 1 : 2)
(* 11 *)       end
(* 12 *)    until taxfree = 0
(* 13 *) end.

(The variable taxfree is a most-recent holder.)

A temporary is typically used for efficiency reasons (e.g., executing a computation, whose value is needed in several places, only once) or to clarify the program (e.g., computing the result in a variable even though the use of a variable would not be absolutely necessary). A temporary is also often used for swapping two data elements of an organizer.

Icon for the role Organizer

An organizer is a data structure 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 that is implemented as an array, 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 or for someother reorganization.

Icon for the role Container

Container is a data structure where elements can be added and removed. The example program implements a stack. The user can add new elements with the command "n" (on lines 11-15) and remove the most recently added element with the command "r" (on lines 16-19). The stack is stored in the array table that acts as a container

(* 1 *)  program Stack(input,output);
(* 2 *)  const SIZE = 10;
(* 3 *)  var table: array [1..SIZE] of integer;
(* 4 *)      top, element: integer;
(* 5 *)      command: char;
(* 6 *)  begin
(* 7 *)     top := 0;
(* 8 *)     repeat
(* 9 *)     write('Enter command: '); readln(command);
(* 10 *)    case command of
(* 11 *)       'n': if top < SIZE then begin
(* 12 *)               write('Enter element: '); readln(element);
(* 13 *)               top := top + 1;
(* 14 *)               table[top] := element
(* 15 *)            end;
(* 16 *)       'r': if top > 0 then begin
(* 17 *)               writeln(table[top], ' removed.');
(* 18 *)               top := top - 1
(* 19 *)            end
(* 20 *)    end
(* 21 *)    until command = 'q'
(* 22 *) end.

(The variable top is a walker, element is a most-recent holder and command is a most-recent holder.)

Icon for the role Walker

Walker traverses a data structure. The example program implements a linked list that has a special head node. The user can add new elements with the command "n" (on lines 13-19) and print all the elements in the list with the command "p" (on lines 20-26). The variable p is a walker: it traverses the whole list (on lines 16 and 24).

(* 1 *)  program List(input,output);
(* 2 *)  type nodePtr = ^node;
(* 3 *)       node = record contents: integer; link: nodePtr end;
(* 4 *)  var head, p, t: nodePtr;
(* 5 *)      element: integer;
(* 6 *)      command: char;
(* 7 *)  begin
(* 8 *)     new(head);
(* 9 *)     head^.link := nil;
(* 10 *)    repeat
(* 11 *)    write('Enter command: '); readln(command);
(* 12 *)    case command of
(* 13 *)       'n': begin
(* 14 *)               write('Enter element: '); readln(element);
(* 15 *)               p := head;
(* 16 *)               while p^.link <> nil do p := p^.link;
(* 17 *)               new(t); t^.contents := element; t^.link :=nil;
(* 18 *)               p^.link := t;
(* 19 *)            end;
(* 20 *)       'p': begin
(* 21 *)               p := head^.link;
(* 22 *)               while p <> nil do begin
(* 23 *)                  writeln(p^.contents);
(* 24 *)                  p := p^.link
(* 25 *)               end
(* 26 *)            end
(* 27 *)    end
(* 28 *)    until command = 'q'
(* 29 *) end.

(The variable head is a fixed value, t is a temporary, element is a most-recent holder, command is a most-recent holder, the field contents is a fixed value and the field link is a fixed value.)


Last update: 22.1.2006

saja.fi@gmail.com
Marja.Kuittinen@UEF.Fi