Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Standard Functions And Procedures


This chapter covers:

  • Intermediate types.
  • Enumerated types.
  • Sub-ranges of enumerated types.


In this chapter I will move on to deal both, with the intermediate types and a more sophisticated use of the elementary types used in the previous two chapters. I will be looking closer at the types, including those, such as, BOOLEAN, and the reserved words WITH and TYPE, as well as the concept of subranges and ordinal types. First of all though, I will go through the use of some of the standard functions and procedures in Pascal.

The Standard Functions

A function is best thought of as a magic box. A value is put in, and a result is returned. Procedures and functions are similar, but the main difference between functions, and procedures is that a function returns a result, and procedures perform other sorts of tasks, but more on procedures later.

ABS(x); CHR(x); ORD(x); SIN(x); COS(x); ROUND(x);
SQR(x); SQRT(x); ODD(x); PRED(x); SUCC(x); STRLEN(x)


The values put into a function are called, in jargon, arguments. The argument type is very important. With some functions their arguments and results have the same type, while others have different arguments and results.

ABS(x)

With this function, a value is put in, in the place of x, and out comes its absolute value. This function takes either REAL or INTEGER, and returns the same type as its result.

  • -15 goes in, and 15 comes out.
  • 15 goes in, and 15 comes out.


Code fragment; ABS(x)

WRITE('Input a number: '); READLN(number); WRITELN; WRITELN('The absolute value of ',number,' is ',ABS(number));

CHR(x)

This function in use takes in an INTEGER, and gives out the character, as related to its position in the ASCII table.

  • 33 goes in, and out comes the character '!'
  • 89 goes in, and out comes the capital letter 'Y'


Code fragment; CHR(x)

WRITE('Enter integer value: '); READLN(number); WRITELN; WRITE('The character ',CHR(number)); WRITELN(' has the ASCII value: ',number);

ORD(x)

This function is the converse of CHR(x). A character is put in, and out comes an INTEGER relating to its position in the ASCII table.

  • '*' goes in, and out comes the number 42
  • 'O' goes in, and out comes the number 79


Code fragment; ORD(x)

WRITE('Enter a character: '); READLN(character); WRITELN; WRITE('The character: ',character); WRITELN('has the ASCII ordinal value: ',ORD(character));

SIN(x)

This is a mathematical function which produces as a result the sine of the value put in. I have read that this function has REAL arguments, and results, but I tried it with an INTEGER, and it had no trouble with the type, but then INTEGER is a subset of REAL.

  • 3.1415926 goes in, and out comes 0.000000
  • 30 goes in, and out comes -0.988032


Code fragment; SIN(x)

WRITE('Enter the number you wish to find the sine of: '); READLN(number); WRITELN; WRITELN('The answer is: ',SIN(number));

COS(x)



This is the mathematical function which produces as a result the cosine of the value put in. As far as argument types go the same applies here as for SIN(x).

  • 15 goes in, and out comes -0.759688
  • 0 goes in, and out comes 1.000000


Code fragment; COS(x)

WRITE('Enter the number you wish to find the cosine of: '); READLN(number); WRITELN; WRITELN('The answer is: ',COS(number));


Of course, with both these functions, field width specifiers could be used in the output lines.

ROUND(x)

This function takes in a REAL number, and produces an INTEGER as the result, rounded, up or down.

  • 3.1415926 goes in, and out comes a 3
  • 3.6473821 goes in, and out comes a 4


Code fragment; ROUND(x)

WRITE('Enter the number you wish to round: '); READLN(number); WRITELN; WRITELN('The result is: ',ROUND(number));

SQR(x)

This function takes in either either a REAL or an INTEGER, and returns its square. That is, the number multiplied by itself.

  • 5 goes in, and out comes 25
  • 3 goes in, and out comes 9


Code fragment; SQR(x)

WRITE('Enter the number you wish to square: '); READLN(number); WRITELN; WRITELN('The result is: ',SQR(number));

SQRT(x)

This function is the converse of SQR(x). It takes in as its argument either a REAL or an INTEGER, and returns its square root.

  • 100 goes in, and out comes 10
  • 50 goes in, and out comes 7.0711


Code fragment; SQRT(x)

WRITE('Enter the number you wish to find the square root of: '); READLN(number); WRITELN; WRITELN('The result is: ',SQRT(number));

ODD(x)

This function is a BOOLEAN function. Its argument type is INTEGER, and the result is a BOOLEAN True or False.

  • 3 goes in, and out comes the value True
  • 4 goes in, and out comes the value False


Code fragment; ODD(x)

WRITE('Enter the number : ') READLN(number); IF ODD(number) THEN WRITELN('The number ',number,' is odd') ELSE WRITELN('The number ',number,' is even');


The next two functions, SUCC(x), and PRED(x) work on any of the ordinal types. These include the types INTEGER, BOOLEAN, CHAR The idea behind the ordinal types is that each member of one of these types has an unique predecessor and a unique successor, except for the first and last member of the type.

PRED(x)

  • 5 goes in, and out comes 4
  • '>' goes in, and out comes '='


Code fragment; PRED(x)

WRITELN('The predecessor of: ',5:0,' is: ',PRED(5):0); WRITELN('The predecessor of : ','>',' is : ',PRED('>'):0);

SUCC(x)

  • 3 goes in, and out comes 4
  • 'd' goes in, and out comes 'e'


Code fragment; SUCC(x)

WRITELN('The successor of : ',3:0,' is : ',SUCC(3):0); WRITELN('The successor of : ','d',' is : ',SUCC('d'):0);

STRLEN(x)

Ok, ok, STRLEN in not a standard Pascal function, but it is a fantastically useful function which takes as its argument a variable of type STRING and returns an INTEGER equal to the number of characters contained in that string. It seems very basic, and not very useful to begin with, but once you get to know it you will see its power.

  • 'help' goes in, and out comes 4
  • 'Niklaus Wirth' goes in, and out comes 13


Notice, that STRLEN() also counts blanks contained within a string.
Suppose: You wanted to have screen output like this:

John Smith..............................24 Hill St, Perth. Alan MacDonald..........................15 Black Rd, Perth. Colin Arnold............................5 Tay St, Dundee.


Where the address is lined up vertically. Before the address is written, there are forty characters required to take up the space, but this means that a variable number of dots are written in each case. So: in the case of the string 'John Smith' in each of the characters from the tenth to the fortieth, you would want to write the '.' character, then write out the address. Here is a small program that will do that.

Program 1

PROGRAM strlen_demo(INPUT,OUTPUT); { demonstration program using the standard function `strlen' } VAR name, address : STRING[25]; I : INTEGER; BEGIN WRITE('Enter name : '); READLN(name); WRITE('Enter address : '); READLN(address); WRITE(name); FOR I := STRLEN(name + 1) TO 40 DO WRITE('.'); WRITELN(address); END. { program strlen_demo }


Notice, that I is initialised to the value of STRLEN + 1, so there will be forty characters in the field. Another useful purpose to which the function STRLEN can be put is to drive inspections of strings. In the first example I showed you, the STRLEN function is used to set the initial value of a FOR loop. In a string inspection algorithm it is used to set the limit of the termination value of the loop. An algorithm is a piece of code which does a specific job.

Program 2

PROGRAM upper_to_lower(INPUT,OUTPUT); { program converts uppercase characters in a string into lowercase characters } VAR customer_name : STRING[20]; I : INTEGER; BEGIN WRITE('Enter customer name : '); READLN(customer_name); FOR I := 1 TO STRLEN(customer_name) DO IF (customer_name[I] >= 'A') AND (customer_name[I] <= 'Z') THEN customer_name[I] := CHR( ORD(customer_name[I]) + (ORD('a') - ORD('A')) ); WRITELN(customer_name); END. { program upper_to_lower }


This piece of code will change all uppercase letters in the string customer_name to lowercase, and the number of loops is controlled by the function STRLEN. The program works by indexing through the string and inspecting each character. If the current character is

>= 'A' AND <= 'Z'


Then is is a capital letter. OK? Now we need to know the difference between the uppercase and lowercase letters in the ASCII table. This is done with

(ORD('a') - ORD('A'))


Then we get the ORD of the current character add to it the result of (ORD('a') - ORD('A')) and then get the CHR of that, and assign that back into the string over the current character. The IF condition, means that all characters other than uppercase letters will be ignored.


Date: 2016-03-03; view: 707


<== previous page | next page ==>
The proof of the pudding | The Standard Procedures
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)