# Ada Programming/Variables

Variables are references that stand in for a value that is contained at a certain memory address.
Variables are said to have a value and may have a data type. If a variable has a type, then only values of this type may be assigned to it. Variables do not always have a type.
A value can have many values of many different types: integers (7), ratios (1/2), (approximations of) reals (10.234), complex numbers (4+2i), characters ('a'), strings ("hello"), and much more.
Different languages use different names for their types and may not include any of the above.

## Assignment statements

An assignment statement is used to set a variable to a new value.
Assignment statements are written as name := value.
The example set the variable X to the integer value of 10. The assignment statement overwrites the contents of the variable and the previous value is lost.
In some languages, before a variable can be used, it will have to be declared, where the declaration specifies the type.
Ada is the same. The declaration is as follows:

## Uses

Variables store everything in your program. The purpose of any useful program is to modify variables.
Without variables, we would not be able to create programs that go much beyond the classic "Hello World!" example. Variables in Ada must be declared. They cannot just magically appear in a program without first having been declared. A variable declaration looks like this:
If a variable is assigned a value when declared, it is said to be initialized. It is possible to declare more than one variable of the same type in one go by comma-separating the variable names:
As you can see, the assignment of a value to a variable is optional. All the compiler needs to know, is what kind of data (the type) you intend the variable to hold. It doesn't care if there's actual data in the variable or not. When you declare a variable, you're basically just asking for a specific amount of memory to be set aside. The actual amount of memory depends, of course, on the declared type. An Integer might take up 4 bytes of memory, and a String (1 .. 10) might take up 10 bytes of memory. The variable name is nothing more than a symbolic name, associated with whatever value that is placed in the memory reserved to the variable.
If we were to assign a value to a variable during its declaration, it would look like this:
Let's try it with some real Ada variables. As seen above, a variable in Ada **must** be declared as pointing to a specific type of data:
Here we declare the variable A_String_Variable as a String type, the length of which is 10 characters, (1 .. 10). Lets see how it looks if we assign a value to A_String_Variable during its declaration:
Here we've first declared and then initialized the A_String_Variable variable.
In the case of the String type, the length can be omitted and we can instead simply write:
Here the assignment of "abcdefghij" implicitly declares A_String_Variable to be of length (1 .. 10), because that is the length of the assigned string.
Variables can be declared as any type, be it String, Integer, records, array or homegrown types. Lets try and declare a variety of variables:
Notice the homegrown Colors type. First we declare the new type, and then we declare the Color variable the of that type. This is one of the biggest strengths of Ada: The ability to declare your own types and subtypes. More can be found on this subject in the Type System article.
Now, let's try and use the above declared variables in an actual program:
When executed, the above program should output the following:
Here we can actually see why variables are called variables: Their values vary.
Notice how we are able to change the data contained in, for example, the A_String variable. When we declare it, we assign the value A_String (the variable is said to be initialized), but a little later we assign another value to A_String: String_A. That right there is the core concept of variables that every beginning programmer must understand: Variables vary. A variable name is just a reference to a place in memory where a a specified type of data resides. The exact data can, and probably will, change a lot during the run of a program, but the variable name and the type remains the same.
If a variable doesn't change during program execution, then we have a better option: A constant. These are the stiff upper lip siblings of variables.

#### Scope

Finally, let's take a short look at variable scope in regard to blocks. When and where are they visible? This is perhaps best understood by a simple program:
The output we get from the above looks like this:
Note how Name is declared as a local variable in the first block. The declaration in the first block does not in any way interfere with the "global" Name variable. If no Name variable is declared for a block, the "global" Name variable is used, as can be seen in the final block, where Thomas Løcke is printed, even though no such assignment has been done in the block.
So variable scope in regard to blocks is very straightforward: Blocks inherit variables from their parent(s) and these can then be overridden locally in the block by declaring the variable anew.

## See also
### Ada Reference Manual

* 3.3 Objects and Named Numbers (Annotated)