TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 55


TURBO-LESSON 12:   FUNCTION APPLICATION - ERROR DETECTION

OBJECTIVES - In this lesson, you will learn about:

1.  Error detection
2.  Using a predefined function
3.  Writing your own function


1.  Error detection.

In the previous lesson,  you found that some input values caused 
a problem.  The function used in PROG11 calculated the cube of a 
number entered.  If the result was outside the range of valid 
integers (for this version of Pascal), a run-time error 
terminated the execution of the program - NOT A NICE EVENT!

If you write programs for others to use, you will have to deal 
with the problem of ERRORS.

There are several approaches to error handling:

(1) Error detection before it happens - prevent the occurrence of 
the error.

(2) Error detection when it happens - take corrective action.

(3) Ignore the error - let the program bomb!

The 3rd is not usually acceptable - but may be o.k. in the early 
stages of program development since you, the programmer, can fix 
the problem.  You may also find alternate ways to program for the 
same result while avoiding the possibility of the error.

The 2nd, error detection when it happens, will be explored later.  
Input/Output errors are typical examples of this class of errors.

In this lesson, you will find ways to detect a problem and 
prevent its occurrence.

##### DO:

In PROG12, examine FUNCTION Cube.

The function has been expanded to detect integers which are too 
small or too large to produce a valid integer cube.  If a number 
is entered which would cause an error, the result is set to 0 
instead of the erroneous result.

TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 56


##### DO:

Run PROG12 several times using the following values for input:

3,  -3,  31, 32, -32, -33, 0

Were all the results as expected?  The inputs,32 and -33, would 
produce cubes out of the range of valid integers, so these two 
should have given results of 0.  

What about 0 as an input?  Did you get the correct result? 

Can you determine whether a result of 0 is valid  (0 input) or 
invalid (input of < -32 or > 31)?

Later in this lesson you will write your own function to deal 
with this problem!



2.  Using a predefined function.

Pascal provides many functions and procedures which are already 
defined.  

Some advantages of using predefined subprograms:

(1)  The subprogram is already debugged.

(2)  The subprogram doesn't take up room in your program.

(3)  You can spend your time on more interesting programming, 
no need to "reinvent the wheel".  

To use a predefined function, you have to know:

(1)  The name of the function

(2)  What goes in  (what values do you provide as input?)

(3)  What comes out  (what result is associated with the function 
name?) 

The absolute value function, ABS, can be used in PROG12 to 
illustrate the use of a predefined function.

What goes in              What comes out

    3 --------> [ABS] --------> 3

   -5 --------> [ABS] --------> 5

The absolute value function provides a positive number of the 
same magnitude as the positive or negative number input to the 
function.  

TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 57


##### DO: 

Add the following statement after the ReadLn(No) statement in 
PROG12:

  WriteLn('Absolute value: ', ABS(No) );

Run the program several times with both positive and negative 
numbers.

NOTE: THERE ARE OFTEN SEVERAL WAYS TO DO THE SAME THING IN 
      PROGRAMMING.  YOU SHOULD BE LOOKING FOR WAYS TO DECIDE 
      WHICH OF SEVERAL PROGRAMMING SOLUTIONS IS BETTER IN A 
      GIVEN CASE.

The next two exercises demonstrate two ways to use ABS in the 
error detection problem.  You should decide which of the two is 
better.  (Maybe neither is better than the present form of 
PROG12).

##### DO:

Change the WriteLn with the reference to Cube to:

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

Test the program with various inputs to make sure the results are 
the same as before.

Is the sign correct on all cubes (using both positive and 
negative inputs)?

Try -32 as an input.  What was the result?   Why?

TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 58


Now, try another way to use the ABS function.

##### DO:

Restore the WriteLn to its original form:

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

Also change the first line of the IF statement in the FUNCTION 
Cube to: 

IF ABS(Number) > 31
     
Test the program with several values.  

Did you get the same results as in the previous exercise?

Which way do YOU think is best?  (Maybe neither because of the 
problem with -32?)

How would you decide whether to use the function, ABS, in the 
main program or in the function, Cube?

Is the action accomplished by ABS of interest to you in getting 
the cube of a number?  If not, it should probably be pushed out 
of the main program and into the subprogram.


3.  Writing your own function.

Now, it's your turn.  Another approach to the error detection 
problem uses a second function, which you are about to write!

Give the function the name:        Has_Valid_Cube

The function will have one input:  Number  of type Integer

The type of the function will be:  Boolean

What the function does:  

If Number would produce a valid cube, the function, 
Has_Valid_Cube, will have the value, TRUE.  

If Number would produce an error, Has_Valid_Cube will have the 
value, FALSE.  
 
TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 59 


##### DO:

Write the function, Has_Valid_Cube.  Place it before the main 
program.  It can be either before or after the function, Cube.

Look at FUNCTION Cube if you need help with the form of the 
function declaration or the IF statement needed.

##### DO:

In the main program, replace the WriteLn which references Cube 
with the following: 

IF Has_Valid_Cube (No)
  THEN
    WriteLn('The cube is: ', Cube(No) )
  ELSE
    BEGIN
      WriteLn('The cube of ',No,' is outside the integer range');
      WriteLn('in this version of Pascal.');
    END;

Test the program with several positive and negative values and 
values which would cause erroneous cubes.

(If you have trouble writing the function, PROG12A is available 
as a sample.  Don't check PROG12A until you have given it a try 
on your own!)

Are there any other improvements you want to make to PROG12?

FUNCTION Cube still checks for invalid inputs.  Is this still 
necessary?

##### DO:

Change FUNCTION Cube so that it does no error checking, just 
calculates the cube of the number input.

Test the program with several values including 0.  

Note that there is no longer any ambiguity when the result is a 
cube of 0.  

