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 the C programming language int, char, float, 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:

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 C.

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 */   #include <stdio.h>
/* 2 */   #define PII 3.14
/* 3 */   int main()
/* 4 */   {
/* 5 */     float r;
/* 6 */     printf("Enter the radius of a circle: ");
/* 7 */     scanf("%f", &r);
/* 8 */     printf("The area of the circle is %.2f\n", PII * r * r);
/* 9 */     exit(0);
/* 10 */  }

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 */  #include <stdio.h>
/* 2 */  int main()
/* 3 */  {
/* 4 */    int multiplier;
/* 5 */    for (multiplier = 1; multiplier <= 10; multiplier++)
/* 6 */      printf("%2d * 3 = %2d\n", multiplier, multiplier * 3);
/* 7 */    exit(0);
/* 8 */  }
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 7) 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 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     float s=0;
/* 5 */     while (s <= 0) {
/* 6 */       printf("Enter the length of the side of a square: ");
/* 7 */       scanf("%f", &s);
/* 8 */     }
/* 9 */     printf("The area of the square is %.2f\n", s * s);
/* 10 */    exit(0);
/* 11 */  }

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 9) the most recent value if it is smaller than the smallest one so far.

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     int i, smallest, number;
/* 5 */     printf("Enter the 1. number: "); scanf("%d", &smallest);
/* 6 */     for (i = 2; i <= 10; i++) {
/* 7 */       printf("Enter the %d. number: ", i);
/* 8 */       scanf("%d", &number);
/* 9 */       if (number < smallest) smallest = number;
/* 10 */    }
/* 11 */    printf("The smallest number was %d\n", smallest);
/* 12 */    exit(0);
/* 13 */  }

(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 9) in it.

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     int count=0;
/* 5 */     float sum=0, number=0;
/* 6 */     while (number != -999) {
/* 7 */       printf("Enter a number, -999 to quit: ");
/* 8 */       scanf("%f", &number);
/* 9 */       if (number != -999) {sum += number; count++; }
/* 10 */    }
/* 11 */    if (count) printf("The mean is %.5f\n", sum / count);
/* 12 */    exit(0);
/* 13 */  }

(The variable count is a stepper and number is a most-recent holder.)

Icon for the role 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 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     float capital, interest;
/* 5 */     int i;  
/* 6 */     printf("Enter capital (positive or negative): "); scanf("%f", &capital);
/* 7 */     for (i = 1; i <=10; i++) {
/* 8 */       interest = 0.05 * capital;
/* 9 */       capital += interest;
/* 10 */      printf("After %2d years interest is %11.2f and total capital is %12.2f\n",
/* 11 */             i, interest, capital);
/* 12 */    }
/* 13 */    exit(0);
/* 14 */  }

(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.

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 11) 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 */   #include <stdio.h>
/* 2 */   #define TRUE 1
/* 3 */   #define FALSE 0
/* 4 */   int main()
/* 5 */   {
/* 6 */     int number=1, sum=0, neg;
/* 7 */     neg = FALSE;
/* 8 */     while (number) {
/* 9 */       printf("Enter a number, 0 to quit: "); scanf("%d", &number);
/* 10 */      sum += number;
/* 11 */      if (number < 0) neg = TRUE;
/* 12 */    }
/* 13 */    printf("The sum is %d\n", sum);
/* 14 */    if (neg) printf("There was at least one negative number in inputs\n");
/* 15 */    exit(0);
/* 16 */  }

(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 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 9).

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     int month, current, previous, biggestDiff;
/* 5 */     printf("Enter the 1. value: "); scanf("%d", &previous);
/* 6 */     printf("Enter the 2. value: "); scanf("%d", ¤t);
/* 7 */     biggestDiff = current - previous;
/* 8 */     for (month = 3; month <= 12; month++) {
/* 9 */       previous = current;
/* 10 */      printf("Enter the %d. value: ", month);
/* 11 */      scanf("%d", ¤t);
/* 12 */      if (current - previous > biggestDiff)
/* 13 */        biggestDiff = current - previous;
/* 14 */    }
/* 15 */    printf("The biggest difference was %d\n", biggestDiff);
/* 16 */    exit(0);
/* 17 */  }

(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 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 8-10), whose value is later meaningless (no matter how long the program would run).

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     int number1, number2, tmp;
/* 5 */     printf("Enter a number: "); scanf("%d", &number1);
/* 6 */     printf("Enter another number: "); scanf("%d", &number2);
/* 7 */     if (number1 < number2) {
/* 8 */       tmp = number1;
/* 9 */       number1 = number2;
/* 10 */      number2 = tmp;
/* 11 */    }
/* 12 */    printf("The bigger of the numbers is %d and the smaller is %d\n",
/* 13 */            number1, number2);
/* 14 */    exit(0);
/* 15 */  }

A temporary is often used for changing the places of two data elements of an organizer.

Icon for the role 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 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     char word[10], tmp;
/* 5 */     int i;
/* 6 */     printf("Enter ten letters: ");
/* 7 */     for (i = 0; i < 10; i++) scanf("%c", &word[i]);
/* 8 */     for (i = 0; i < 5; i++) {
/* 9 */       tmp = word[i];
/* 10 */      word[i] = word[9-i];
/* 11 */      word[9-i] = tmp;
/* 12 */    }
/* 13 */    for (i = 0; i < 10; i++) printf("%c", word[i]);
/* 14 */    printf("\n");
/* 15 */    exit(0);
/* 16 */  }

(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: 29.4.2004

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