# Names and values

Programs need to keep track of information when performing their tasks. A program computing your salary needs to keep track of your salary, your tax, and most likely also some information about your bank account. For this purpose, programs often use variables, which can be assigned values.

Variables can be viewed as named storage places, where values of different types can be stored. Among the different types you will find numerical values, such as the number $$\pi$$, but also strings of characters, like the string "Hello, world" that was used in the program in Figure 1.

## Values

A value can be assigned directly to a variable, using an explicit value. Such an explicit value is called a literal. A character literal, in this case the character literal b, can be assigned to a variable with the name a using the assignment

a = 'b'


As can be seen, the assigment is done by placing the character b, enclosed in single quotes, at the right hand side of an equals sign. When a value is assigned to the variable, the variable is automatically being declared. In this case, since a character value was assigned to the variable, the variable is declared to be a variable that can store strings of characters. A variable of this type can hold values which are strings, like "Hello, world", consisting of characters such as the letters a to z, but also special signs, like semicolon (;), comma (,), and exclamation mark (!).

A variable can also be assigned a numerical value. There are two kinds of numerical values. One kind is referred to as floating point values, and the other kind is referred to as integer values. A floating point variable represents decimal numbers and an integer variable represents whole numbers. As an example, the decimal value for $$\pi$$, rounded by the programmer to 7 decimals, can be assigned to a variable named pi, using the assignment

pi = 3.1415927


## Named storage places

The program in Figure 3 contains the three variables i, pi, and a. Each of these variables can hold a value. In this way, a variable can be viewed as a storage place for a value. Since a variable has a name, it can be viewed as a named storage place.

A variable also has a type. As an example, the variable i is an integer variable. The type of a variable determines the type of values that are allowed to be stored. Hence, the variable i can store integer values.

As demonstrated in the program in Figure 3, variables can be assigned literal values. Variables can also be assigned values that are the result of computations. These computations can be performed using other variables, as well as literal values.

As an example, consider a program for calculating the area and the circumference of a circle. As we have learned in school, the area of a circle is computed using the number $$\pi$$ and the radius of the circle. Using the notation $$r$$ for the radius and the notation $$a$$ for the area, the formula reads $$a = \pi r^2 \quad \quad \quad (1)$$ The circumference, here denoted $$c$$, of a circle is calculated using the circle diameter. Using the fact that the circle diamater is the radius multiplied by two, the formula for calculating the circumference reads $$c = \pi \cdot 2 r \quad \quad \quad (2)$$ Calculations corresponding to (1) and (2) can be done in a program, using a variable named area for the area a, a variable named circ for the circumference, and a variable named radius for the radius $$r$$. In addition, a variable named PI can be used to represent $$\pi$$, by assigning a numerical value to the variable as

PI = 3.1415927


The variable radius is used, together with the variable PI, to calculate the area as

area = PI*radius*radius


The variable radius is also used in the calculation of the circumference, as

circ = PI*2*radius


## Some computations

Computer programs often perform computations. Here we consider some examples illustrating computations.

A program can be used to calculate future values of an investment. Consider an example where an initial sum $$s$$ is invested. It could then be of interest to calculate the future value of the investment. Assuming that the value is increased by $$r$$ percent each year, the value after a number of years can be calculated.

If we do, as is common in mathematics-oriented books, denote the number of years by a variable, for example called n, the value of the investment, after n years, can be calculated as $$s_n = s \left (1 + \frac{r}{100} \right )^n \quad \quad \quad (3)$$ We note that the formula (3) also applies to other situations, for example when calculating your salary after n years, given an annual raise of r percent.

In a program, designed to calculate a person's salary after a given number of years, we can use a variable named salary to represent the salary. The raise $$r$$, divided by 100, could be represented by a variable, initalised to an annual raise of 5 percent, as

s_raise = 0.05


Using a variable n_years to represent the number of years, the salary after 5 years and after 10 years, starting at an intial salary of 3000, can be computed by first assigning values to the variables salary and n_years, and then performing calculations according to (3). Program code for performing these operations is shown in Figure 5.

salary = 3000
n_years = 5

salary = salary * pow(1 + s_raise, n_years)
print "my salary in %d years is %g" % (
n_years, salary)

salary = salary * pow(1 + s_raise, n_years)
print "my salary in %d years is %g" % (
2*n_years, salary)


Figure 5. Calculation of salary after 5 and 10 years, given an intial salary and a yearly raise.

This the Python view - other views are C - Java

The program code in Figure 5 uses a function called pow, for the purpose of calculating the exponent in (3). The function pow is part of a Python library with mathematical functions.

The program code in Figure 5 can be placed in a program and executed. The result of running the code inside a program becomes

my salary in 5 years is 3828.84
my salary in 10 years is 4886.68