Differences

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

Link to this comparison view

lynplexs:tutorial:constants [2014/05/25 16:52] (current)
Line 1: Line 1:
 +====== Constants ======
 +Constants can be provided within a program in two ways.  They can either be literal values or named constants.
 +
 +===== Named Constants =====
 +The best example of a named constant always seems to be //​PI//​. ​ It is a constant value although, because it has a large, possibly infinite number of decimal places, it is usually limited to only a few of those. ​ //PI// can be variously defined as 3.142, 3.14159265 or to a value with even greater accuracy. ​ It can even be defined as 22/7 for simple calculations. ​ However we define it //PI// itself is still a constant value.
 +
 +In the LynPlex interpreter constants are defined as:
 +  const PI = 3.14159265
 +  ​
 +//**Note:** In LynPlexS this would be defined as://
 +
 +  $$pi = 3.14159265
 +
 +//and in LynPlexC as://
 +
 +  const PI [as datatype] = 3.14159265
 +  ​
 +Assigning a name to a constant value means that you do not have to remember what the actual value is.  For example:
 +  c = 2 * PI * r
 +  ​
 +
 +===== Constants in Expressions =====
 +Constants may be used anywhere in an expression. ​ For example.
 +  put "The value of PI is" PI
 +will show:
 +  The value of PI is 3.14159265
 +
 +===== Changing Constants =====
 +Although constants are intended to remain the same throughout the program, the primary purpose of defining a value as a constant is to stop it being accidentally modified.
 +
 +Having defined //PI// as shown above, the statement:
 +  PI = 123456
 +will be rejected.
 +
 +If you really want to, you can change any constant value provided that you redefine it as a constant. ​ For example, if you want PI to have a lower precision enter: ​
 +  const PI = 3.142
 +or
 +  const PI = 22/7
 +
 +===== Page Links =====
 +
 +|< 100% 35% - 28% - 35% >|
 +| <= [[literals]] ​ ^ |  [[start]] ​ ^ |  [[variables]] => |
  

lynplexs/tutorial/constants.txt · Last modified: 2014/05/25 16:52 (external edit)