Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






The Standard Procedures

So far we have only looked at some of the Pascal standard functions. I have already said that procedures and functions are similar. So now we will look at the main standard procedures in Pascal. The only ones you will really use are the following.

WRITE WRITELN READ READLN


As strange as it may seem, these are procedures, and they have arguments, just like functions. These procedures are quite easy to use, but there are some subtle points worth going into. The arguments in both functions and procedures are alternatively called parameters. A fuller explanation of parameters comes later in the book.

WRITE and WRITELN

What do WRITE and WRITELN do? Both of these procedures have what is called a parameter list this list can contain variables, constants, expressions and text. It may also contain a call to a function. The line,

WRITE('Enter a number : ');


contains text only. This means that everything between the two apostrophes is then written on the screen as an input prompt. The line,

WRITELN('Here is the answer ',answer);


has both text and a variable in its parameter list. The result of which is that the text is written to the screen, and the value of the variable is also written to the screen. A constant would be written out in the same way. The line,

WRITELN('The answer is ',(2 * answer) + SQRT(16));


contains text, and an expression. The expression also contains a call to the standard function SQRT (square root). When an expression is contained in the parameter list the expression is evaluated and the result is written to the screen in the required place. Both of these two standard procedures may have no arguments.

WRITE; WRITELN;


The main difference between the WRITELN and the WRITE is that the WRITELN returns the cursor to the next line, and the WRITE keeps it on the same line. Which means that the above two lines of code would produce one blank line of screen output. Run the next small program to see this.

Program 3

PROGRAM writes_1(INPUT,OUTPUT); { this program shows difference between WRITE and WRITELN } BEGIN WRITELN('================'); WRITE; WRITELN; WRITELN('================'); END. { program writes_1 }

READ and READLN

These two standard procedures are a little more tricky to understand. For that reason we will start with a program.

Program 4

PROGRAM reads_1(INPUT,OUTPUT); { this program demonstrates the difference between the READ and READLN } VAR first, second, third, fourth, fifth, sixth : INTEGER; BEGIN WRITE('This is the prompt for the reads : '); READ(first,second,third); WRITELN(first,second,third); WRITE('This is the prompt for the readln : '); READLN(fourth,fifth,sixth); WRITELN(fourth,fifth,sixth); WRITE('Press <enter> to continue : '); READLN; END. { program reads_1 }


Now when you run this, I want you to type in at the first prompt

1 2 3 4 5


with a <space> after each number and a <return> after the '5'. At the next prompt type

6 7 8 9 0


again with a <space> after each number and a <return> after the '0'. The two calls of WRITELN will produce,



1 2 3 4 5 6


Why? Well the explanation comes form the way in which these two procedures work. If you supply more data items to the procedure READ than it has variables in its parameter list these extra data items will be held onto until the program encounters another READ or READLN. So the first three data items are passed into the variables first, second, and third in the parameter list of the READ, but as I said, READ causes the rest of the data items to be held until the next READ of READLN is encountered by the program, this is why the '4' and '5' end up being passed the variables fourth and fifth in the parameter list of the READLN and the next data item to be encountered, the '6' is then passed into sixth. But what happens to the rest of the data? Well see the next program for the answer. In the next program I have switched the READ and READLN. Run this with the same data, and in the same way.

Program 5

PROGRAM reads_2(INPUT,OUTPUT); { this program demonstrates the difference between the READ and READLN } VAR first, second, third, fourth, fifth, sixth : INTEGER; BEGIN WRITE('This is the prompt for the readlns : '); READLN(first,second,third); WRITELN(first,second,third); WRITE('This is the prompt for the reads : '); READ(fourth,fifth,sixth); WRITELN(fourth,fifth,sixth); WRITE('Press <enter> to continue : '); READLN; END. { program reads_2 }


This program will produce the output,

1 2 3 6 7 8


Again, why? Well just as a READ holds on to extra data items. The READLN ignores any extra data items in the input line. So the '4' and the '5' are ignored in the first data line. So as far as the READ is concerned, the '6', '7' and '8' are the only data items it knows about. Remember though, that READ will cause the program to hold onto the '9' and the '0' for the next READ or READLN and that may cause you a problem later on in the program. This is a possible source of bugs, this bug is avoided by being very careful about the number of data items being supplied to a program, and using the READLN to clear the data input ready for the next lot of input.

Intermediate Types

BOOLEAN

As previously seen, the type boolean has only two values. It can either be TRUE or FALSE. The two situations that boolean values appeared in earlier were in conjunction with the IF - THEN - ELSE statement, when the condition between the IF, and the ELSE yielded a boolean result, and in the WHILE loop where the condition also yields a boolean result.


Date: 2016-03-03; view: 210


<== previous page | next page ==>
Standard Functions And Procedures | NOT a boolean operator
doclecture.net - lectures - 2014-2017 year. Copyright infringement or personal data (0.006 sec.)