Ada Programming/Variables

Assignment statements are written as.

X = 10;

Ada is the same. The declaration is as follows:

X : Integer = 10; Do_Something (X); ;

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:

Variable_Name : Variable_Type [:=Optional Value]

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  in one go by comma-separating the variable names:

Variable_A, Variable_B : Variable_Type [:=Optional Value]

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 ) 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. An  might take up 4 bytes of memory, and a   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:

Variable_Name : Variable_Type := Variable_Value

Let's try it with some real Ada variables. As seen above, a variable in Ada **must** be declared as pointing to a specific  of data:

Here we declare the variable  as a   type, the length of which is 10 characters,. Lets see how it looks if we assign a value to  during its declaration:

Here we've first declared and then initialized the  variable.

In the case of the  type, the length can be omitted and we can instead simply write:

Here the assignment of  implicitly declares   to be of length , because that is the length of the assigned string.

Variables can be declared as any, be it  ,  ,  ,   or homegrown types. Lets try and declare a variety of variables:

Notice the homegrown  type. First we declare the new type, and then we declare the  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:

A_String B_String String_A String_B 1: 1 2: 2  3: 3  1: 10  2: 20  3: 30  10  RED Thomas Løcke

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  variable. When we declare it, we assign the value  (the variable is said to be initialized), but a little later we assign another value to  :. 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  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  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:

1:Thomas Løcke 2:Dwight S. Miller 3:Thomas Løcke 4:Thomas Løcke

Note how  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"  variable. If no  variable is declared for a block, the "global"   variable is used, as can be seen in the final block, where   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.

Ada Reference Manual


| Programación en Ada/Objetos