 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  48
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          

          TURBO-LESSON 7.  REPEAT STATEMENT

          OBJECTIVES - In lesson 7 you should learn about:

          

          1.  CHARacter variables
          2.  BOOLEAN variables
          3.  REPEAT statement

          


                               1.  CHARacter Variables.

          The reserved word, CHAR, is used to declare a variable of character
          type.  A variable of type CHAR can be used to refer to or store a
          single character.

          VAR
             Alpha : CHAR;

          

          Alpha is declared to be a variable which can be used to store any
          of the characters in the character set.  This includes the upper
          and lower case alphabet, the digits, 0 to 9, special characters
          such as #, $, %, *, and the rest of the 256 characters in the PC's
          character set.

          ##### DO:

          Examine PROG7.

          A variable named Response, of type CHAR, is used to store the
          character entered in response to a multiple choice question.

          ##### DO:

          Set the lower window to Output and run the program several times,
          entering wrong responses, and the correct response, D.  Also try
          lower case d.

          ##### DO:

          Study the first IF statement in PROG7.

          IF (Response = 'D') OR (Response = 'd')
              THEN . . .
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  49
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          The character 'D' must be enclosed in single quotes in the program.
          Note that you enter the character as input data without quotes when
          running the program.

          Notice the compound condition using OR to combine two simple
          conditions.  This condition will be true if either  or both of the
          simple conditions are true.  The correct response, D, is checked in
          both upper and lower case to make responding easier.

          ##### DO:

          Modify the IF statement to accept A or B as the right response.
          Assume that the correct answer is A or B.  (Ignore lower case
          responses to keep the statement shorter.)


                                2.  BOOLEAN Variables.

          BOOLEAN variables can have only two values, TRUE or FALSE.   The
          value of a condition, TRUE or FALSE, may be stored in a BOOLEAN
          variable for later use.

          VAR
            Correct_Response : Boolean;

          In PROG7, the Boolean variable, Correct_Response, is used to store
          the truth value (TRUE or FALSE) of the condition in the first IF
          statement.  If the correct character, 'D' or 'd', is entered, TRUE
          is stored in Correct_Response.  If anything else is entered, FALSE
          is stored.

          Actually, TRUE and FALSE are stored as 1 and 0 to take up less
          space, but you can always view a BOOLEAN variable as having a value
          of TRUE or FALSE.

          ##### DO:

          Identify the condition in the second IF statement in PROG7.

          Since BOOLEAN variables can only have two values, TRUE or FALSE,
          and conditions always evaluate to the same two values, a BOOLEAN
          variable may be substituted for a condition.

          If would be permissible, but unnecessary to write the IF statement:

          IF Correct_Response = TRUE
             THEN . . .

          ##### DO:

          Modify the IF statement as indicated above and run the program to
          verify that the IF still works exactly as before.
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  50
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          There is another way to assign the correct value to the variable,
          Correct_Response.

          ##### DO:

          Replace the FIRST IF statement in PROG7 with the following
          statement: Correct_Response := (Response = 'D') OR (Response =
          'd');

          Run the program.

          How does the program change when you run it?  (If it doesn't do
          exactly as before, maybe you typed it wrong, or replaced the second
          IF instead of the first?)

          Since the condition on the right of the := must be evaluated by the
          computer and assigned a value of TRUE or FALSE, this value can be
          stored directly in a BOOLEAN variable without using the IF
          statement.

          ##### DO:

          Add Response and Correct_Response to the Watch window and trace the
          program.

          When does Correct_Response receive it's correct value?


                                 3.  REPEAT Statement.

          In a previous lesson, you learned that there are three ways to
          sequence the execution of statements in PASCAL: SIMPLE SEQUENCE,
          SELECTION STRUCTURES, and REPETITION STRUCTURES.

          One of the statements used for REPETITION is REPEAT . . UNTIL.  The
          form of the REPEAT statement is:

          REPEAT
            Statement 1;
            Statement 2;
              .
              .
              .
            Statement n
          UNTIL condition;

          Statements 1, 2, . . . , n will be executed repeatedly until the
          condition becomes true.  This implies that the condition is
          checking something that can be changed by the statements 1 to n.
          If this is not so, the statements will be repeated forever!

          In PROG7, you are prompted to respond to the multiple choice
          question.  A REPEAT statement controls the block of statements
          which prompt for a response, and then check the response.  The
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  51
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          block of statements will be repeated until a correct response makes
          the UNTIL condition true.

          ##### DO:

          Change the condition in the UNTIL in PROG7 to:
               UNTIL 'A' = 'B';

          Run or Trace the program.

          Does the program correctly identify a correct response?  What
          happens then.

          (Use ctrl-c or ctrl-Scroll-Lock to stop the program.)

          ##### DO:

          Change the condition again to:
               UNTIL 'A' = 'A';

          How many times is the Repeat loop executed?

          Notice that the statements in a REPEAT structure are ALWAYS
          executed at least once.  Even if the UNTIL condition is TRUE before
          entering the REPEAT, the condition is not checked until the end of
          the statements in the REPEAT block.
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  52
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          

          TURBO-LESSON 8.  CASE STATEMENT

          OBJECTIVES - In lesson 8 you will learn about:

          1.  Block statements
          2.  CASE statement


                                 1.  Block Statement.

          As noted in an earlier lesson, the form of the IF statement is:
               IF condition THEN statement_1 ELSE statement_2;

          IF the condition is true, statement_1 is executed, otherwise
          statement_2 is executed.  However, a single statement may not
          always get the job done.

          The Block statement (also called a Compound statement) allows you
          to substitute a multiple statement block anywhere a simple
          statement is acceptable.  The form of the Block statement is:
               BEGIN Statement_1; Statement_2 END;

          You can include as many statements as you like between the BEGIN
          and END.

          Notice that the main body of a Pascal program could be viewed as a
          single statement!  That single statement is a block statement:

          BEGIN
            Statement_1;
            Statement_2;
              .
              .
              .
            Statement_n;
          END.

          To illustrate the use of a Block statement in an IF statement,
          consider the following problem:

          If the value of I is greater than the value of J, swap the two in
          memory (a common problem when sorting data).

          If you happen to have a single statement to swap the values of two
          memory locations, the IF statement might be:

          IF I > J
            THEN SWAP(I,J);

          Later, you will learn how to make up your own "statements" (by
          creating functions and procedures), but for now, the way to swap I
          and J is:
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  53
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          IF I > J
            THEN
              BEGIN
                Temp := I;
                I := J;
                J := Temp;
              END;
          

          Notice that the form of this IF statement is still correct:
               IF condition THEN statement;

          The statement, in this case, is a Block statement, rather than a
          simple statement.

          ##### DO:

          Take a look at the program called TEST1.

          This program is provided to make it quicker for you to test new
          statements and concepts.  TEST1 has some integer variables and
          character variables declared and the main BEGIN END.  All you need
          to do to test a statement, or group of statements, is edit them
          into the test program and run the program.

          ##### DO:

          Insert the following statements between the BEGIN and END of
          program TEST1:

          Write('Enter two numbers ');
          ReadLn(I, J);
          WriteLn('I=', I, ' J=', J);

          ##### DO:

          Set the lower window to Output and run the program.

          ##### DO:

          Between the ReadLn and WriteLn statements you just entered, add the
          IF statement to swap I and J if I is larger:

          IF I > J
            THEN
              BEGIN
                Temp := I;
                I    := J;
                J    := Temp;
              END;

          Run the program several times using several pairs of input numbers
          to test the program.  Does the "swap" work right?
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  54
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          ##### DO:

          Switch the lower window to Watch.  Add the following to the watch
          window: I, J, Temp and (I > J).  Trace the program.

          It may not be clear why three statements are needed to "Swap" two
          numbers.  Why not just use

               I := J;
               J := I;

          ##### DO:

          Remove the statement Temp:=I.  Change the statement J:=Temp to
          J:=I;  Trace the program several times with different values until
          you see why this doesn't work.

          Tracing while watching variables will often help you uncover weak
          logic and false assumptions in your programs.

          ##### DO:

          Restore the IF statement to it's original form with the 3-statement
          swap using Temp.  Run it to make sure it works right before going
          on.

          What would happen if you forgot the BEGIN and END to enclose
          several statements that belong to an IF statement?

          ##### DO:

          Remove the BEGIN and END in the IF statement and run the program
          several times.

          Does the "swap" still work right?

          ##### DO:

          Run the program with 7 and 22 as input.  (These don't need
          swapping.)

          Did the swap still work right?  What happened?

          Without the BEGIN and END to make the three statements appear as
          one,  the statements "appear" as follows to Pascal:
               

          IF I > J
            THEN
              Temp := I;
          I := J;
          J := Temp;
          
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  55
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          Only the statement, Temp := I, is controlled by the IF condition.
          The two statements, I:=J; and J:=Temp; are executed for all values
          of I and J.  They are no longer under the control of the IF.  When
          your IF statements don't perform quite right, check to be sure
          which statements are controlled by the IF.

          ##### DO:

          Trace the program.  Watch the variables carefully as each statement
          is traced.


                                  2.  CASE Statement.

          First, a bit of review from an earlier lesson:

          Program sequencing is done in Pascal with

            (1) Simple Sequence,   one statement follows another,
          
            (2) Selection Structures,
                  IF  statement for one-way and two-way selection,
                  CASE statement for many-way selection,
          
            (3) Repetition Structures,
                  REPEAT statement,
                  WHILE statement,
                  FOR statement.

          The CASE statement is useful when there are more than two actions
          or statement sequences needed.  The form of the CASE statement:

          CASE variable OF
              value_1  : Statement_1;
              value_2  : Statement_2;
                .
                .
                .
              value_n  : Statement_n;
            ELSE
              Statement;
          END; {CASE}

          Note the following:

          The variable must be a simple type such as Integer, CHAR, BOOLEAN.
          (REAL is not allowed).

          The values used to determine which Statement to execute must be of
          the same type as the variable.

          The values may be a constant, a list of constants, or a subrange
          such as 1..10 (all integers from 1 to 10).
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  56
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          How it works :

          (Refer to the "form of the CASE statement" above.)

          If the variable has a value of "value_1" then Statement_1 is
          executed.  If "value_2" then Statement_2, . . .

          If the value of the variable matches none of the values, the
          statement following the ELSE is executed.

          There must be an END to mark the end of the CASE statement. (It's a
          good idea to add the comment {CASE} after the END).

          ##### DO:

          Use the editor to examine PROG8.

          This is the same problem as in the previous lesson, with a bit more
          programming flexibility derived from the CASE statement and the
          block statements.

          Notice that the list of variables in the CASE statement allow
          appropriate responses for acceptable responses: A, a, B, b, C, c
                                       correct responses: D, d
                                  unacceptable responses: anything else.

          ##### DO:

          Run the program with the Output window to see how it works.

          ##### DO:

          Switch the lower window to Watch.  Remove any watch variables left
          from earlier programs (Alt-B, R).  Add Response and
          Correct_Response as watch variables.

          ##### DO:

          Trace the program.  Watch the action of the CASE statement.  (You
          may need to move the Cursor down in the program to force the CASE
          statement up in the Edit window.  This will allow you a better view
          of the action of the CASE statement.)

          ##### DO:

          Modify the CASE statement in PROG8 to accept C as the best answer.

          Run the program.  Did it work?
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  57
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          

          TURBO-LESSON 9.  FOR STATEMENT

          OBJECTIVES - In lesson 9, you will learn about:

          1.  Controlling field width in Write statements
          2.  FOR statement


                   1.  Controlling Field Width in Write Statements.

          Spacing of output in previous lessons has been done using spaces in
          string constants, such as, 'I= '.  The space after the = will be
          printed as part of the string.

          There is another way to add spaces.  When listing the items to
          write in a Write or WriteLn statement, ":n" can be added to specify
          how many characters the item is to occupy in the output.

          For example:

          WriteLn(I:4);

          The value of I would be printed in a space 4 characters wide.

          ##### DO:

          Withe the bottom window set to Output, run Program TEST1 several
          times with the following statements in the main part of the
          program:

          Write('Enter a Number: ');
          ReadLn(I);
          WriteLn('[', I:4, ']');

          Look closely at the output.  Are the numbers "left-justified" or
          "right-justified"?

          If I has a value of 23, and 23 prints in columns 1 and 2 of the 4
          column field, it is left-justified.  If the 23 appears in columns 3
          and 4 of the 4 column field, it is right-justified.

          What happens if you enter a number that is too large to fit the
          field specified?

          ##### DO:

          Run the program several times using the following values for I:

          123,   1234,   -1234,   12345
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  58
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



                                  2.  FOR Statement.

          In the previous lesson, you were reminded of the various ways to
          control the sequencing of program actions.  One of the REPETITION
          structures is the FOR statement.  The forms of the statement are:

          FOR variable := lowvalue TO highvalue DO
               Statement;   {May be a block statement with BEGIN END}

          FOR variable := highvalue DOWNTO lowvalue DO
               Statement;

          ##### DO:

          Run PROG9 several times to see how it works.

          Experiment with both positive and negative numbers.

          What happens if High is smaller than Low?

          ##### DO:

          Run PROG9 using 5 for Low (first number entered), 3 for High?

          What was the result?

          Also try 5 for both values.  How many times is the loop repeated if
          Low = High?

          What results would you expect if you entered 3.4 for Low and 5.3
          for High?

          ##### DO:

          Run the program with the values 3.4 and 5.3.

          Were you correct in predicting the outcome?

          Can constants be used instead the variables Low and High?

          ##### DO:

          Edit the first FOR statement in PROG9 as follows:

          FOR Index := 1 to 5 DO

          Run the Program.

          Is it possible to use expressions for Low and High?

          ##### DO:

          Change the FOR statement to:
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  59
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          FOR Index := Low + 1 TO High DO

          Run the Program.

          ##### DO:

          Trace the program with High, Low, and Index in the watch window.
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  60
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          

          TURBO-LESSON 10.  WHILE STATEMENT

          OBJECTIVES - In lesson 10 you will learn about:

          1. CONSTant declaration
          2. WHILE statement
          3. Delay timing loop


                               1.  CONSTant Declaration.

          In the declaration section of the Pascal programs in previous
          lessons, only VAR and PROGRAM have appeared.

          CONST is used to declare constants and assign values to them.

          The form is:

          CONST  Constant_Name_1   =  Value_1;
                 Constant_Name_2   =  Value_2;
                         .
                         .
                         .
                 Constant_Name_n   =  Value_n;
          

          Some examples:

          CONST  PI            =  3.14159;
                 Fahr_Freeze   =  32;
                 Cels_Freeze   =  0;
                 Message_1     =  'This is a string constant';

          ##### DO:

          Run PROG10 a time or two with the Output window to see what it
          does.

          ##### DO:

          Edit the constant declaration, Wobble_Size = 5;
                                   to    Wobble_Size = 10;

          Run the program.  What effect did you see from the change?

          ##### DO:

          Change the value of the constant Left_Edge to 20 and run the
          program.

          To understand how Left_Edge and Wobble_Size work, look at the
          WriteLn statement:
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  61
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          WriteLn(': ':Left_Edge,  First:Index,  Last:2,  Message);

            1.  ': ' is printed in the right 2 columns of a field whose
                 width is determined by the value of the constant, Left_Edge.

            2. First initial is printed in the rightmost column of a field
                of width, Index.  Note that Index is changing in the FOR
                loop, so the width of this field changes.

            3. Last initial is printed next in the rightmost column of a
                2-character wide field.

            4. The string constant, Message, is printed immediately to the
                right of Last initial.

          ##### DO:

          ##### DO:

          In the WriteLn statement described above, change Last:2  to
          Last:Index.  The statement should read:
          
            WriteLn(':  ':Left_Edge, First:Index, Last:Index, Message);

          ##### DO:

          Run the program to see the effect of the change you just made.

          ##### DO:

          Run the program several times with the values of the constants
          reset to various values.  (LIVE DANGEROUSLY - try some wild values:
          50 for Wobble_Size or 70 for Left_Edge.  Your CRT won't explode!
          At least mine hasn't - yet!)


                                 2.  WHILE Statement.

          The three Pascal REPETITION Structures are:
            (1)  WHILE
            (2)  REPEAT
            (3)  FOR
          

          The form of the WHILE statement is:
             WHILE condition DO statement;

          Note that the statement is often a block statement and appears:

          WHILE condition DO
            BEGIN
              Statement_1;
              Statement_2;
                 .
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  62
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



                 .
                 .
              Statement_n;
            END;

          The WHILE is executed "while" the condition is true.

          Contrast the REPEAT which is executed while the condition is false,
          UNTIL the condition becomes true.

          ##### DO:

          Run PROG10 again, using 0 as the number "less than 8".  (You may
          want to load a fresh copy of PROG10 if you have made a lot of
          changes to the program.)

          How many times was the loop executed?

          You could substitute REPEAT for WHILE in PROG10.

          ##### DO:

          Replace:     WHILE Count > 0 DO
             with:     REPEAT

              add:     UNTIL Count < 1;   after:   END; {WHILE}

          (Notice that you don't have to remove the BEGIN END when you change
          the loop from WHILE to REPEAT.  They are not needed in the REPEAT
          statement, but a block statement is always acceptable wherever a
          simple statement can be used.)

          Run the program several times to verify that it still works the
          same as with the WHILE loop.

          ##### DO:

          Run the program with 0 for the "number less than 8".

          How many times was the loop executed?

          Notice that the WHILE loop may be executed 0 times, but the REPEAT
          loop is always executed at least once, since the condition is not
          checked until the end of the loop.

          NOTE: YOU SHOULD BE ON THE LOOKOUT FOR SUCH CONTRASTS BETWEEN
          "INTERCHANGEABLE" STATEMENTS.  CHOOSING THE BEST STATEMENT TO USE
          IN EACH CASE IS BASED ON THIS KIND OF AWARENESS.

          ##### DO:

          Change the REPEAT loop back to the original WHILE loop.(An easy way
          would be to load a fresh copy of the original PROG10.)
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  63
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          Change the WHILE statement to accept only Last initials less than
          'G'.

               WHILE Last < 'G' DO

          Run the program.   Try it once with H (uppercase) for a last
          initial.  Then try it with A (uppercase).  How did it work?

          The WHILE loop didn't terminate at all when you used an A for the
          last initial - you had to abort the program, right?  (If it is
          still executing, use Ctrl-C to stop it.)

          One of the things needed in controlling loops: something in the
          loop must be related to the condition.

          In the original WHILE loop, the Count was being decreased in the
          loop.

          When the condition was changed to (Last < 'G'), the loop no longer
          has any effect on the condition.

          Either the loop will not be executed at all, if Last is = or > 'G',
          or it will execute indefinitely if Last is < 'G'.

          ##### DO:

          Change the WHILE statement to:

               WHILE (Last < 'G') AND (Count > 0) DO

          Run the program several times using last initials before and after
          G in the alphabet.

          ##### DO:

          Run the program with last initial of 'a' (lower-case a).

          How many times did the loop execute?

          Why?

          The ASCII value of 'G' is 71.  The ASCII value of 'a' is 91.  So
          'a' is > 'G'.


                                3.  Delay Timing Loop.

          You may have noticed the "wobble" proceeds faster in one direction
          than the other.  This is due to a timing delay in the first WHILE
          loop.  (You may need to use a number larger than 5000 in the delay
          statement for faster computers.  Notice that the variable, Delay,
          is declared to be a "longint" type.  This is an integer variable
          which can store larger numbers.)
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  64
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          FOR Delay := 1 to 5000 DO; {Do nothing, except loop}

          This looks a little strange?   Shouldn't there be a statement after
          the DO?

          Pascal has a "null" statement for just such times as this.  When a
          statement is required, but none is present, Pascal just substitutes
          its "null" or "do-nothing" statement.

          ##### DO:

          Change the delay in the loop from 5000 to 20000 and run the
          program.

          ##### DO:

          Add a timing loop to the other WHILE loop and run the program.

          How did it work?

          One last little twist on timing loops:

          ##### DO:

          Change the Wobble_Size to 10.

          Change both delay statements to:

               FOR Delay := 1 to 5000 * Index DO;

          Run the program.  Note that delays don't have to be constant.  This
          delay varies as the FOR loop progresses!
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  65
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          

          TURBO-LESSON 11.  INTRODUCTION TO FUNCTIONS

          OBJECTIVES - In this lesson, you will learn about:

          1.  Pascal Subprograms
          2.  FUNCTION declaration
          3.  How a FUNCTION Works
          4.  Providing Input to the FUNCTION


                                1.  Pascal Subprograms.

          Why bother with subprograms?  Doesn't that just complicate
          programs?  You will find that subprograms are used to simplify,
          rather than complicate programs.

          The example programs in earlier lessons have been rather small.  As
          programs grow in size and complexity, it is essential that you
          utilize the appropriate tools to simplify the programming effort.

          The subprogram is one of the most powerful tools for simplifying a
          complex program.  Subprograms may be viewed as "building blocks"
          for constructing programs.

          As an illustration of the building block approach, consider a
          program to enter and sort names for a phone directory, and then
          print the sorted directory.

          The main program might be:

               BEGIN
                 EnterNames;
                 SortDirectory;
                 PrintDirectory;
               END.

          This shows the "big picture",  what the 3 main steps are.

          The subprograms, EnterNames, SortDirectory, and PrintDirectory may
          each contain up to a page of program statements.  If all of this
          processing were done in the main program, it might be difficult to
          understand the program as a whole.

          As you learn to write Pascal programs, you will find Pascal
          subprograms rather easy to write - they are almost identical to
          programs!

          There are two types of subprograms in Pascal: FUNCTIONS and
          PROCEDURES.   Both types may be either user-defined (written by
          you) or pre-defined as a part of Pascal.

          In this lesson you will work with user-defined FUNCTIONS.
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  66
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          Most of what you learn about FUNCTIONS also applies to PROCEDURES.


                               2.  FUNCTION Declaration.

          Below is a simplified structure of a Program, Function, and
          Procedure:

          PROGRAM         FUNCTION       PROCEDURE
          LABEL           LABEL          LABEL
          CONST           CONST          CONST
          TYPE            TYPE           TYPE
          VAR             VAR            VAR

          FUNCTION(s)     FUNCTION(s)    FUNCTION(s)  (0 or more FUNCTIONS)
          PROCEDURE(s)    PROCEDURE(s)   PROCEDURE(s) (   and PROCEDURES  )
          
          BEGIN           BEGIN          BEGIN
          (processing     (processing    (processing
           statements)     statements)    statements)
          END.            END;           END;
          

          Notice that they look very similar.

          One difference, visible here, is the END of the processing block: A
          period follows the END of a PROGRAM block, but a semi-colon follows
          the END of a FUNCTION or PROCEDURE block.


                              3.  User-defined Function.

          We start with a very simple function to illustrate several key
          concepts.

          ##### DO:

          Look at PROG11.  The Function declaration is:

               FUNCTION GiveMe5:Integer;

          The significance of each part of this declaration is:

          FUNCTION       Type of subprogram, a FUNCTION, not a PROCEDURE.       
                         The two don't work in quite the same way.

          GiveMe5        The NAME of the FUNCTION.  This gives you a way to     
                         refer to this function.  The statement,                
                         WriteLn(GiveMe5), in the main program uses the name,
                         GiveMe5, to use this function.

          INTEGER        The name, GiveMe5, is associated with a memory         
                         location which can store an Integer.  Somewhere        
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  67
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



                         within the BEGIN END block of the FUNCTION, GiveMe5    
                         must be given an Integer value to store.

          First, observe how the program sequences the execution of
          statements.

          ##### DO:

          Trace PROG11, watching the highlight progress through the main
          program, to the FUNCTION, and back to the main program.

          The function name, GiveMe5 must be given a value in the function.
          What do you think would happen if no value were given to the name,
          GiveMe5?

          ##### DO:

          Remove the statement, GiveMe5:=5; in the FUNCTION.  The function
          BEGIN END block now has no active statements.

          ##### DO:

          Run the program.  What was printed?  Mine printed 22005.  Yours
          might print 0 or something else.  This is just whatever happens to
          be in the memory location allocated for GiveMe5.


                          4.  Providing Input to the FUNCTION

          You would no doubt agree that the function, GiveMe5, in the
          previous section is pretty useless.  You could use the statement
          WriteLn(5); in the main program to do the same thing as
          WriteLn(GiveMe5);

          Most useful functions operate on data which is provided as input to
          the function.

          PROG11A uses a function with input.  A number is input to the
          Function named Cube.  The function calculates the cube of the
          number and stores the result in a memory location associated with
          the name, Cube.

          ##### DO:

          Examine FUNCTION Cube in PROG11A using the editor.

          FUNCTION Cube(Number:Integer) : Integer;

          The FUNCTION declaration above means:
          

          FUNCTION         Type of subprogram, a FUNCTION.
          
          Cube             The name of the FUNCTION.
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  68
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          
          (Number:Integer) An integer "parameter" called Number is used
                           within the FUNCTION. This is the number to be        
                           cubed.
          
          : Integer;       Type of value to be assigned to Cube.

          ##### DO:

          Study the connection between the following two statements:

          In FUNCTION Cube:

          FUNCTION Cube(Number:Integer) : Integer;

          In PROG11A:

          WriteLn('The cube is: ', Cube(No) );

          Cube(No) in the statement above invokes (calls) the FUNCTION Cube
          to operate on the number called No.

          PROG11A,  No ----->  Number, in FUNCTION Cube.

          The value of the variable called No in PROG11A is provided to the
          FUNCTION to use as a value of its variable called Number.

          ##### DO:

          Put the variables Number and No in the watch window.  Trace the
          Write and ReadLn in the main program.  Enter 3.  Stop when the
          WriteLn statement is highlighted.

          Notice that No now has the value 3.  Number (in the Function) is an
          "Unknown identifier".

          ##### DO:

          Trace the WriteLn statement--press F7 once.

          Observe that 2 things happened:

               1.  The program execution transferred from the WriteLn
               statement in the main program to the beginning of the Function
               Cube.

               2.  Number now has the value 3, which it received from No in
               the main program.

          ##### DO:

          Trace the rest of the program.  Watch the program execution return
          to the main program and end there.
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  69
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          ##### DO:

          Set the lower window to Output and run PROG11A several times using
          the following values for input:

          3,  -3,  0

          31   (cube should be 29791)

          32   (cube should be 32768)  Is it?  Remember this problem from an
          earlier lesson?  You will explore some techniques for detecting
          this problem in the next lesson.

          Now, let's see what happens if you change some of the things in the
          FUNCTION and the reference to it.

          What would happen if you used a constant instead of No in the
          reference, Cube(No)?

          ##### DO:

          Change the WriteLn statement to:
               WriteLn('The cube is: ', Cube(3) );

          Run the program several times with different input values.

          Do you get the same result no matter what you input?

          Notice that the variable, No, which you input, is no longer used in
          the FUNCTION reference, Cube(3).

          Can you use expressions in the reference to the function?

          ##### DO:

          Change the FUNCTION reference to Cube(No - 2).

          Run the program several times with input values:

          5     (No - 2) is 3, so cube should be 27.

          1     (No - 2) is -1, so cube should be -1.

          -1    (No - 2) is -3, so cube should be -27.

          Do integer expressions work o.k?

          What if you tried to use a non-integer value in the Cube(No)
          reference?

          ##### DO:

          Modify the WriteLn statement to:
               WriteLn('The cube is: ', Cube('This is a string') )
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  70
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          Run the program.

          Did the compiler complain?

          Error 26: Type mismatch.

          Note that the cursor is positioned at or near the problem.

          The problem: 'This is a string' was given to the function to use as
          an integer value,  but it is a string, not an integer.

          A good way to learn to recognize errors, is to introduce one error
          at a time and check the results.

          Let's look at some errors which you might make in setting up the
          FUNCTION itself.

          ##### DO:

          Change the type of Number to Char.  The FUNCTION declaration should
          appear:

          FUNCTION Cube(Number:Char) : Integer;

          What happens when you attempt to run or compile the program?

          Error 41: Operand types do not match operator.

          Notice that the cursor stops, not at the line you modified, but at
          the line:

          Cube := Number * Number * Number;

          Why?

          There is nothing wrong with the line you changed.  It may not do
          what you want it to, but to the computer, it is just an Integer
          FUNCTION with a character for input.

           The problem appears (to the compiler) when an attempt is made to
          multiply a character times a character in the line marked by the
          cursor.  At this point the compiler alerts you to the problem.

          DEBUGGING NOTE: DON'T FORGET, THE CURSOR POINTS TO THE PLACE WHERE
          THE ERROR WAS DETECTED BY THE COMPILER.  THE CAUSE OF THE ERROR MAY
          BE ELSEWHERE!

          What would happen if you inadvertently designated the function as
          something other than Integer?

          ##### DO:

          Change the FUNCTION declaration to:
 
        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  71
            Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00



          FUNCTION Cube(Number:Integer) : Char;

          Run the program.    What results did you get?

          Error 26 again - Type mismatch.

          Again, the cursor points to the calculation.

          This time though, the calculation itself is o.k.    The problem
          occurs when an attempt is made to assign the integer result to the
          Character FUNCTION, Cube.

          ********************** SHAREWARE REMINDER ************************

          Are you enjoying the lessons?  Have you sent your payment yet?  If
          you are like me, no matter how good your intentions, you may
          neglect to send your check if you wait until you have finished the
          lessons and set them aside!  (Just a friendly reminder--I would
          like to write more tutorials.  Your check makes that possible.)
