home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
ENTERPRS
/
CPM
/
UTILS
/
S
/
TPTUTOR2.ARC
/
TL11.TXT
< prev
next >
Wrap
Text File
|
1985-06-28
|
11KB
|
416 lines
TURBO-LESSONS
A TURBO Pascal Tutorial
Version 1.01
Set # 2 - Lessons 11 - 17
by Lyle M. Faurot
June 27, 1985
Copyright (C), 1985 by Lyle Faurot. All rights reserved.
TURBO is a trademark of Borland International.
î
INTRODUCTION TO TURBO-LESSONS
When Borland International ran their first ads for TURBO
PASCAL my reaction was "You gotta be kidding - they can't do
that". But, it seems they did! I've been enjoying this
programming system ever since. Apparently a great many other
programmers are either programming in TURBO or would like to
learn how. TURBO-LESSONS were designed to meet that need.
Each TURBO-LESSON is a bite-size tutorial which introduces
one or more new concepts. Each of the lessons are presented in
the same format which includes the OBJECTIVES of the lesson, and
the TUTORIAL portion of the lesson. Most lessons direct you to
work with a sample program which is provided with these lessons.
To begin, you should print the TURBO-LESSONS before loading
TURBO.
****************************************
D I S T R I B U T I O N N O T I C E
****************************************
TURBO-LESSONS are being distributed as USER-SUPPORTED software.
Suggested donation, $5.00, for this second set, lessons 11-17,
may be sent to:
Lyle Faurot
Box 494
Moorhead, MN 56560
COPYING BY ORGANIZATIONS
Clubs and other non-profit organizations may copy these lessons
for their members, with the following conditions:
1. No charge is made for TURBO-LESSONS. A distribution charge
may be made to cover the price of the diskette.
2. Club members are informed that TURBO-LESSONS are distributed
as user-supported software.
3. TURBO-LESSONS are distributed unmodified.
î
T A B L E O F C O N T E N T S
(Set # 2, Lessons 11 - 17)
Page
Introduction
Distribution Notice
TURBO-LESSON 11: INTRODUCTION TO FUNCTIONS
Pascal subprograms . . . . . . . . . . . . . . . . . 50 *
FUNCTION declaration . . . . . . . . . . . . . . . . 50
User-defined function . . . . . . . . . . . . . . . . 51
TURBO-LESSON 12: FUNCTION APPLICATION - ERROR DETECTION
Error detection . . . . . . . . . . . . . . . . . . . 55
Using a predefined function . . . . . . . . . . . . . 56
Writing your own function . . . . . . . . . . . . . . 58
TURBO-LESSON 13: STRINGS
Strings . . . . . . . . . . . . . . . . . . . . . . . 60
String replacement statement . . . . . . . . . . . . 60
Predefined string function, LENGTH . . . . . . . . . 63
TURBO-LESSON 14: INTRODUCTION TO PROCEDURES
PROCEDURE declaration . . . . . . . . . . . . . . . . 65
Using a procedure . . . . . . . . . . . . . . . . . . 66
Using parameters . . . . . . . . . . . . . . . . . . 67
A counter with error checking . . . . . . . . . . . . 68
TURBO-LESSON 15: INTERACTIVE SCREEN HANDLING
Setting up a data entry screen . . . . . . . . . . . 70
Being nice to users - ClrScr . . . . . . . . . . . . 71
Getting around the screen - GotoXY . . . . . . . . . 72
Screen messages and accepting user input . . . . . . 73
TURBO-LESSON 16: REAL NUMBERS
Range of real numbers . . . . . . . . . . . . . . . . 74
Input/Output of real numbers . . . . . . . . . . . . 75
Calculations with real numbers . . . . . . . . . . . 76
Calculations with integers and real numbers . . . . . 78
TURBO-LESSON 17: A TIMING FUNCTION
Include files . . . . . . . . . . . . . . . . . 79
A timing function . . . . . . . . . . . . . . . . . . 80
Improving timing accuracy . . . . . . . . . . . . . . 81
Sources of errors in timing . . . . . . . . . . . . . 82
* (Note - page 42 was the last page of lesson 10. Some page
numbers were left unused to make it easier to incorporate
the changes you suggest.)
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 50
TURBO-LESSON 11: INTRODUCTION TO FUNCTIONS
OBJECTIVES - In this lesson, you will learn about:
1, Pascal Subprograms
2. FUNCTION declaration
3. User-defined function
1. Pascal Subprograms.
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 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 a user-defined FUNCTION.
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.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 51
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.
##### DO:
Examine FUNCTION Cube in PROG11 using the editor.
FUNCTION Cube(Number:Integer) : Integer;
The FUNCTION declaration above means:
FUNCTION Type of subprogram, a FUNCTION, not a PROCEDURE.
Cube The name of the FUNCTION.
Cube must be assigned a value by one of the
statements in the FUNCTION BEGIN END block.
(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 PROG11:
WriteLn('The cube is: ', Cube(No) );
Cube(No) in the statement above invokes (calls) the FUNCTION Cube
to operate on the number called No.
PROG11, No -----> Number, in FUNCTION Cube.
The value of the variable called No in PROG11 is provided to the
FUNCTION to use as a value of its variable called Number.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 52
##### DO:
Determine how Cube obtains its value in the FUNCTION.
Cube := Number * Number * Number;
This statement cubes the value in the variable Number (which
Number received from the variable, No, in the main program) and
assigns the result to the FUNCTION name, Cube.
The WriteLn statement prints this resulting value in the position
indicated by the reference to Cube(No) in the WriteLn list.
##### DO:
Run PROG11 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).
What about expressions in the reference to the function?
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 53
##### 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') )
Run the program.
Did the compiler complain?
Error 44: Type mismatch.
When you press ESC, the cursor stops at the offending reference.
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.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 54
##### 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 47: Operand type(s) does not match operator.
When you press ESC, 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:
FUNCTION Cube(Number:Integer) : Char;
Run the program. What results did you get?
Error 44 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.
î