
           INTERMEDIATE SECTION D                                FILE INTER.D
                                    MORE ON VARIABLES 
                                    *****************

          In PRIMER, section c, we found that groups of characters, such as 
          words, names, sentences, numbers etc. can be, and often are, 
          represented by another character or group of characters, There are 
          many reasons for this, some of which are listed below.

          1) To save typing.- Put cls$=CHR$(27)+"E"+CHR$(27)+"H" at the start 
          of your program. As you probably know, PRINT CHR$(27)+...etc. is the 
          command to clear the screen and home the cursor, but as it is now 
          represented by the variable cls$, all we need to do is to type  
          PRINT cls$ at any point in the program when it is desired to clear 
          the screen; a definite saving in typing
           
          2) To control 'constants'.-     Suppose that you are writing a 
          program involving prices of goods; cost prices, selling prices, VAT 
          etc. You would need to quote the VAT rate perhaps several times and 
          use it in various calculations. If the VAT rate were to change you 
          would be faced with finding each reference to the VAT rate and 
          changing it,always with the chance that you might miss one or two. 
          Instead you would, near the beginning of your program put   vat=15, 
          then use the variable ,vat, in place of the figure. If VAT then went 
          down from 15% to 12% you would need to change your program in one 
          place only  vat=12, and the deed is done, quickly and accurately. In 
          the example you could do the same with the profit markup if you 
          anticipated changing the markup from time to time.

          3) To generate values automatically .- Variables always take the 
          latest value given to them . Consider this piece of program

              10 FOR a=1 to 9
              20 PRINT 8*a;
              30 NEXT

          Lines 10 and 20 form a loop where each time it loops the value of   
          a   changes.  Initially  a=1,then 2 etc.  Thus line 20 will print 
          8,16,24 etc. Although the example is simple, the principle is 
          powerful, giving endless possibilities.

          Another way in which a numeric variable can be made to change 
          automatically may, at first, seem rather odd. You will often see in 
          programs such lines as ;
              60 a=a+1 

          What nonsense, you might say, how can  a  equal more than itself. 
          The answer lies in the = sign, which in this context means 
          'represents' or 'becomes'  and is another way of the variable 
          changing, in this case incrementing by 1 each time the line 60 is 
          run. The variable can be made to change in countless ways, to suit 
          your needs, simply by altering the equation. Don't worry about it, 
          just accept it!. 

          4) To represent a set of data items in an array.-  a 'subscripted' 
          variable is used such as data$(y), where y is the subscript which 
          itself represents a set of numbers equivalent to the number of data 
          items. Thus if 10 items are in the array they could be represented 
          by data$(1),data$(2).........to data$(10). This brief explanation is 
          enlarged upon in the section on arrays. 

          5) To store information typed in from the keyboard after an INPUT 
          command as in the following example

              400 INPUT " Please type your name ", nam$

          when line 400 is executed, the request "Please type your name " will 
          appear on screen. The reply which is typed in will be stored in the 
          variable nam$.
                            ++++++++++++++++++++++++   
            The above examples show some of the many ways in which variables 
          are used but if you examine a few program listings you will see 
          variables used in a great variety of ways. I hope, however that the 
          above descriptions will give you a start to understanding the logic 
          of the programming. 

          You will soon find that for localised routines, such as counting 
          loops you can use and reuse the same variable, and some programmers 
          habitually use a variable for specific tasks. For example you may 
          see a loop starting -  100  FOR i=1 to 10 -  Later you see 
          - 240  FOR i=2 to 40 ; a line starting a second loop, and so on 
          throughout the program, using i each time. From what has been said 
          before it will be easy to explain this. After the first loop, i will 
          retain the last value, 10, but on execution of line 240, i will be 
          given the new value 2, until the second loop is finished, when it 
          will have the value 40. So it is quite safe to reuse a variable for 
          such a series of tasks which are self-contained

          For more important values it is better to choose a variable which is 
          brief yet reminds one of it's use, for example :-
                  nam$="Name of employee", 
                  titl$= "Title of book" ,
                  total = Total number of records, etc.

          In 3) above I mentioned that variables take the last value assigned 
          to them, and it is important to understand the implications of this 
          statement fully, because it is a property which can be exploited in 
          several ways.  Suppose you write a line   500   a=45, then  a  will 
          remain equal to 45 until a new value is allocated or until the 
          variable is cancelled. All values of variables are made equal to 0 
          when the program is started with the RUN command. This is why we use 
          the GOTO command to restart a program when we do not want to lose 
          all variable values. Variable values are also lost with some other 
          commands; LOAD, CHAIN, NEW and SYSTEM for instance. In fact any 
          command which interferes drastically with the program.

          When a program ends, or you stop it, or it stops itself to register 
          a fault in the program, the values assigned to each variable still 
          remain in memory. This can be a most useful aid in debugging a 
          program, writing a program, or even just finding out how it works. 
          Just type PRINT x at the BASIC prompt and press [RETURN] and the 
          latest value of x will be displayed on the screen. Used astutely 
          this can tell you a lot about the current state of the program.

          Types of variable  You probably understand now that a variable has  
          *****************  to match the value that it represents, and that                       
          the type or class of variable is formed by its suffix. Any attempt 
          to use an incorrect type of variable will be met with an error 
          message "type mismatch". The variable types are ( using var as the 
          base ):-

              var$          Strings enclosed in double quotes e.g "Hello"
              var           Numbers in general - no suffix
              var%          Whole numbers (Integers) 
              var!          Decimal numbers - Single precision
              var#          Decimal numbers - Double precision

          We see that there are four types of variable to represent numbers 
          and only one for strings. This is simply because there are distinct 
          ways in which numbers can be stored and used in  BASIC.    A 
          variable with suffix % will only store whole numbers, and if asked 
          to represent a number with decimal  places, will ignore the decimal 
          places and store only the integer part.For many programs it is a 
          necessary requirement. For example, if you were writing a game 
          involving dice, it would not be helpful if the dice rolled a 4.325.
              The suffixes ! and # are appended to variables which store 
          numbers of single and double precision respectively. If no suffix is 
          appended the default is single precision.  Suffice it to say that it 
          affects the accuracy of any calculations carried out, as double 
          precision arithmetic uses more significant figures than single 
          precision. 
              The three types of variable (%,!,#) are stored in 2,3 and 7 
          bytes respectively, which means that their storage capacity, and 
          therefore the number of significant figures that can be handled by 
          each type varies considerably. Integer variables accommodate numbers 
          from -32768 to 32767 and an attempt to exceed these limits will get 
          the error message Overflow. Unless you specifically need integers 
          you are best advised to use single precision. For larger numbers or 
          greater accuracy use double precision.      

          DEFINT,DEFSNG,DEFDBL,DEFSTR are four command words that can be used, 
          *************************** usually near the start of a program to 
          define letters as variable types. For example, the line:- 
                          10  DEFINT a-g: DEFSTR h-m
          means that any time letters a to g, or words beginning with these 
          letters,  are used as variables they represent integers and when 
          letters h to m, or words beginning with letters h to m, are used 
          they represent strings. DEFSNG and DEFDBL define, as I'm sure you 
          have guessed , single and double precision. 
              When using this method of defining variable types NO SUFFIXES 
          ARE USED and can lead to confusion unless care is taken. This method 
          is best used when only one variable type is to be used. It is 
          usually preferred to define variables with suffixes as you go along, 
          and sometimes you may wish to use the same letter for two types of 
          variable. Maybe you have an integer represented by a string variable 
          a$ and you wish to convert it to a simple integer for use in a maths 
          sum. You would write the line:-
                          700  a=VAL(a$) . To the computer a and a$ are 
          completely different one from another, and yet to us they are 
          linked, which helps the memory. 

          Unfortunately it is difficult to illustrate these commands in this 
          program, because once used the commands cannot be reset and would 
          interfere with the remainder of the program. However, a short 
          program, DEFINT.BAS, is included and can be LOADed and RUN to 
          demonstrate the method of use.

          SWAP  is a command to exchange the contents of two variables, and is 
          ***** written         SWAP variable1,variable2 , but will only work 
          if the two variables are of the same type. 

          I always use lower case for variables to avoid confusion with 
          keywords, which BASIC always makes INTO upper case


          End of file INTER.D
es to avoid confusion with 
          keywords, which BASIC always makes IN