Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Useful Functions And Procedures

This chapter covers:

  • A selection of some useful functions and procedures.
  • Layering of language.
  • Validation of user input.
  • Special cases.

In this chapter I will show you some useful functions and procedures which I have written. I hope they will be of some use, and also give you ideas of how to write your own. The first, is a procedure which wraps up the selection sort algorithm into a procedure.

{----------------------------------------------------------} PROCEDURE selection_sort(VAR test_array : INTEGER_ARRAY; MAX : INTEGER); { this procedure performs the selection sort algorithm on the array "test_array" } VAR { local variables } I, J, temp : INTEGER; begin FOR I := 1 TO MAX DO FOR J := (I+1) TO MAX DO IF test_array[J] < test_array[I] THEN { swap elements } BEGIN temp := test_array[J]; test_array[J] := test_array[I]; test_array[I] := temp; END; END; { procedure selection_sort } {----------------------------------------------------------}

The procedure has two formal parameters. The first is test_array which is the array to be sorted, and you will notice that this is a variable parameter, because it is a data structure, and because we are very likely to be changing the contents of the array around. The second is the size of the array to be sorted, MAX. This is supplied as a value parameter. To use this procedure in the main program would require a call such as this:

selection_sort(test_array,size);

In the actual call of the function or procedure, the variables used are called the actual parameters. They can have different identifier names from the formal parameters which are those in the function or procedure heading itself.

{----------------------------------------------} FUNCTION power(base: REAL; exponent : INTEGER) : REAL; { this function will return the result of any real number to any power } VAR { declare local variables } I : INTEGER; product : REAL; BEGIN product := 1; FOR I := 1 TO exponent DO product := product * base; power := product; END; { function power } {----------------------------------------------}

This is a very powerful, ha ha, function. Given a REAL number the base, and an INTEGER, the exponent, it will calculate the result with really only three lines of code. The variable product has to be initialised to 1, because if to were initialised at 0 then the line,

product := product * base

would produce the result of zero every time. This is not a very obvious point, and it may be a potential bug which could be very difficult to fix. This is therefore an obvious case of being sure of what it is you are trying to do with your function or procedure.

Below, function power is incorporated into a program.

Program 1

PROGRAM powers_1(INPUT,OUTPUT); { a demonstration program of how to call your own functions } VAR any_number, answer : REAL; any_other_number : INTEGER; { ------------------------------------------------------------ } FUNCTION power(base: REAL; exponent : INTEGER) : REAL; { this function will return the result of any real number to any power } VAR { declare local variables } I : INTEGER; product : REAL; BEGIN product := 1; FOR I := 1 TO exponent DO product := product * base; power := product; END; { function power } { ------------------------------------------------------------ } BEGIN { MAIN PROGRAM } WRITE('This program calculates the '); WRITELN('powers of numbers'); WRITE('Enter the base number : '); READLN(any_number); WRITE('Enter the exponent number : '); READLN(any_other_number); answer := power(any_number,any_other_number); WRITE('The result is : ',answer:2:4); END. { program powers }

Notice than in this example I have used different names for the actual parameters in the function call from those in the formal definition of the function. This gives you a clue towards understanding about actual parameters. By this I mean that the variable names, used in the call, need not be the same variable names used in its definition. An interesting and more powerful use of the function count_character could count the occurrences of each character contained within a sentence. Thinks! I'll need a FOR loop driven by ASCII characters. We have done that already (see program char_list). This FOR loop will use as a loop counter variable, a different character at each loop, and this can be compared with the elements of the inspected string, and a count of each occurrence of that character kept.



Program 2

PROGRAM character_count(INPUT,OUTPUT); { this program makes a more powerful use of the function count_characters. It could form the basis of a text analysing program } TYPE SENTENCE : STRING[50]; VAR { declare global variables } the_string : SENTENCE; a_char : CHAR; number_of_characters, count : INTEGER; { ------------------------------------------------------------ } FUNCTION count_characters(VAR in_string : SENTENCE; the_character : CHAR) : INTEGER; { this function returns an integer equal to the number of times "the_character" appears in "in_string" } VAR { declare local variables } I, character_count : INTEGER; BEGIN character_count := 0; FOR I := 1 TO STRLEN(in_string) DO IF (in_string[I] = the_character) THEN character_count := character_count + 1; count_characters := character_count; END; { function count_characters } { ------------------------------------------------------------ } BEGIN { MAIN PROGRAM } WRITE('Enter the text : '); READLN(the_string); WRITELN('The text contains'); FOR a_char!' TO '~' DO BEGIN count := count_characters(the_string,a_char) IF (count > 0) THEN BEGIN number_of_characters := number_of_characters + count; WRITELN(count,' character "',a_char); END; END; WRITE('There are ',number_of_characters:2); WRITELN(' characters in the sentence not counting spaces.'); END. { program character_count }

Date: 2016-03-03; view: 861


<== previous page | next page ==>
Using a variable parameter as an in parameter | Inserting in order into an array
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)