Differences

This shows you the differences between two versions of the page.

Link to this comparison view

variables_and_constants [2011/12/15 20:19]
thomaslocke
variables_and_constants [2013/02/21 08:31] (current)
thomaslocke [Variables]
Line 17: Line 17:
   Variable_A, Variable_B : Variable_Type [:=Optional Value]   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 //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. ​+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: If we were to assign a value to a variable during its declaration,​ it would look like this:
Line 23: Line 23:
   Variable_Name : Variable_Type := Variable_Value   Variable_Name : Variable_Type := Variable_Value
  
-Lets 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:+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:
  
 <code ada> <code ada>
Line 29: Line 29:
 </​code>​ </​code>​
  
-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 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:​
  
 <code ada> <code ada>
Line 35: Line 35:
 </​code>​ </​code>​
  
-Here we've first declared and then initialized the ''​A_String_Variable'' ​variable. ​+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:+In the case of the //String// type, the length can be omitted and we can instead simply write:
  
 <code ada> <code ada>
Line 43: Line 43:
 </​code>​ </​code>​
  
-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.+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:+Variables can be declared as any //type//, be it //String////Integer////records////array// or homegrown types. Lets try and declare a variety of variables:
  
 <code ada> <code ada>
Line 61: Line 61:
 </​code>​ </​code>​
  
-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 on this subject in the [[types and subtypes]] article. ​+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 [[types and subtypes]] article. ​
  
-Now, lets try and use the above declared variables in an actual program:+Now, let'​s ​try and use the above declared variables in an actual program:
  
 <code ada> <code ada>
Line 138: Line 138:
 Here we can actually see why variables are called variables: Their values vary.  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.+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. 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.
Line 145: Line 145:
 Constants are an important tool to help make your programs more reliable and maintainable. A constant is, just like a variable, a reference to a specific //type// of data, but where they differ from variables is in their constant nature. Variables vary, constants are constant. When declared and initialized,​ the data they reference is static and can no longer be altered. If you try to alter a constant, the compiler will complain, loudly. Constants are an important tool to help make your programs more reliable and maintainable. A constant is, just like a variable, a reference to a specific //type// of data, but where they differ from variables is in their constant nature. Variables vary, constants are constant. When declared and initialized,​ the data they reference is static and can no longer be altered. If you try to alter a constant, the compiler will complain, loudly.
  
-Lets see how we declare and initialize a constant:+Let'​s ​see how we declare and initialize a constant:
  
 <code ada> <code ada>
Line 157: Line 157:
 </​code>​ </​code>​
  
-The only differences from a variable are the reserved ​''​constant'' ​keyword and the fact that a constant **must** be initialized when declared. Other than that, declaring and initializing constants is done exactly the same way as variables and it works for all //types//:+The only differences from a variable are the reserved ​//constant// keyword and the fact that a constant **must** be initialized when declared. Other than that, declaring and initializing constants is done exactly the same way as variables and it works for all //types//:
  
 <code ada> <code ada>
Line 195: Line 195:
 ===  Named numbers ​ === ===  Named numbers ​ ===
  
-The attentive reader will have noticed that the ''​Pi'' ​constant used above is declared without a //type//. Such constants are called [[http://​www.adaic.com/​standards/​05rm/​html/​RM-3-3-2.html|named numbers]] and their //type// is called an [[http://​www.adaic.com/​standards/​05rm/​html/​RM-3-4-1.html|universal type]]. There are four kinds of //universal types//:+The attentive reader will have noticed that the //Pi// constant used above is declared without a //type//. Such constants are called [[http://​www.adaic.com/​standards/​05rm/​html/​RM-3-3-2.html|named numbers]] and their //type// is called an [[http://​www.adaic.com/​standards/​05rm/​html/​RM-3-4-1.html|universal type]]. There are four kinds of //universal types//:
  
   * universal_integer   * universal_integer
Line 202: Line 202:
   * universal_access   * universal_access
  
-You cannot explicitly declare an object to be of type //​universal_integer//​. But what you can do is declare an object to be a constant //named number//. These may take values of any size or precision, as opposed to the types derived from these, for example ​''​Integer'' ​and ''​Float''​. The universal types are not bound by the same constraints as the types derived from them:+You cannot explicitly declare an object to be of type //​universal_integer//​. But what you can do is declare an object to be a constant //named number//. These may take values of any size or precision, as opposed to the types derived from these, for example ​//Integer// and //Float//. The universal types are not bound by the same constraints as the types derived from them:
  
 <code ada> <code ada>
Line 209: Line 209:
 </​code>​ </​code>​
  
-The ''​Named_Large_Int'' ​constant is of type //​universal_integer//,​ because the literal ​''​4_294_967_296'' ​is an integer. Had we instead written:+The //Named_Large_Int// constant is of type //​universal_integer//,​ because the literal ​//4_294_967_296// is an integer. Had we instead written:
  
 <code ada> <code ada>
Line 215: Line 215:
 </​code>​ </​code>​
  
-then ''​Named_Real'' ​is of the type //​universal_real//​ because the literal ​''​4.294_967_296'' ​contains a [[http://​www.adaic.com/​standards/​05rm/​html/​RM-2-4.html|point]].+then //Named_Real// is of the type //​universal_real//​ because the literal ​//4.294_967_296// contains a [[http://​www.adaic.com/​standards/​05rm/​html/​RM-2-4.html|point]].
  
 ====  Scope  ==== ====  Scope  ====
-Finally ​lets take a short look at variable scope in regards to blocks. When and where are they visible? This is perhaps best understood by a simple program:+Finally ​let'​s ​take a short look at variable scope in regards to blocks. When and where are they visible? This is perhaps best understood by a simple program:
  
 <code ada> <code ada>
Line 252: Line 252:
   4:Thomas Løcke   4:Thomas Løcke
  
-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.+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 regards 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. So variable scope in regards 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.
  

Navigation