Home Random Page



Using a variable parameter as an in parameter

Well I told you that using a value parameter makes the program make a copy of the variable in another part of memory, and with a variable parameter the program makes a direct link to the original variable. The difficulty is that if we pass in data structures like strings, records and arrays as value parameters then the program will have to wait until a copy is made. Imagine making a copy of an array of 200,000 elements in to a procedure. With each element of that array comprising of a record with six fields. If this procedure is only performing a search algorithm on the array, an algorithm which will only take a tiny fraction of a second to perform. This it is clearly not very efficient to make a copy of this array first. So with these data structures in general we pass them as variable parameters, even though we only mean to use it as an in parameter. As the first demonstration of a program with a procedure call I have written this next one. This program does nothing, but demonstrate the uses of parameters and how the order of formal parameters and actual parameters must be the same.

  • Remember
    formal = procedure or function heading.
    actual = procedure or function call.

A more in depth explanation comes later on in the chapter.

Program 4

PROGRAM pegs_and_holes(INPUT,OUTPUT); { a demonstration program which shows how important procedure parameter matching is } TYPE SQUARE_SHAPE = STRING[15]; ROUND_SHAPE = STRING[15]; VAR square_peg : SQUARE_SHAPE; { here is a square peg } round_peg : ROUND_SHAPE; { here is a round peg } square_hole : SQUARE_SHAPE; { here is a square hole } round_hole : ROUND_SHAPE; { here is a round hole } {-------------------------------------------------} PROCEDURE insert_pegs(VAR square_hole : SQUARE_SHAPE; VAR round_hole : ROUND_SHAPE); BEGIN WRITELN('This procedure does absolutely nothing'); END; { procedure insert_pegs } {-------------------------------------------------} BEGIN { MAIN PROGRAM } WRITE('Enter a string : '); READLN(square_peg); round_peg := square_peg; WRITE('round_peg is the same as square_peg '); WRITELN(round_peg = square_peg); insert_pegs(square_peg,round_peg);{ insert_pegs(round_peg,square_peg);} WRITELN('The square peg is ',square_peg); WRITELN('The round peg is ',round_peg); END. { program pegs_and_holes }

Once you have compiled and run the program, I want you to comment out the first call of the procedure insert_pegs and delete the curly-brackets which comment out the second call of the procedure insert_pegs. Recompile the program, and you will get a compilation error, something to the effect that the formal parameters and the actual parameters do not match. What it is that doesn't match is the types of the parameters. In the second call the order of the actual parameters has been reversed. With this next program I will show you how to write a program which calls your own functions and procedures.

Program 5

PROGRAM demo_calls(INPUT,OUTPUT); { this program counts the number of specified characters in a customer's name --- and searches through an array for that customers name } CONST MAX = 1000; TYPE A_NAME : STRING[25]; AN_ADDRESS : STRING[50]; A_NUMBER : STRING[10]; A_BALANCE : REAL; DETAILS = RECORD name : A_NAME; address : AN_ADDRESS; number : A_NUMBER; balance : A_BALANCE; END; CUSTOMER_ARRAY = ARRAY[1..MAX] OF DETAILS; VAR { these are global variables } J : INTEGER; { global counter } character : CHAR; account_array_one : CUSTOMER_ARRAY; found : BOOLEAN; customer_name : A_NAME; an_index : INTEGER; { ------------------------------------------------------------ } PROCEDURE search_array(VAR value_found : BOOLEAN; VAR index : INTEGER; VAR customer_name : STRING; VAR account_array_one : CUSTOMER_ARRAY); { this procedure will search the array customer_array for customer_name returning a boolean and an integer, equal to its position in the array } VAR { declare local variables } I : INTEGER; BEGIN value_found := FALSE; I := 1; WHILE NOT(value_found) AND (I <= MAX) DO BEGIN IF (account_array_one[I].name = customer_name) THEN value_found := TRUE ELSE I := I + 1; END; index := I; END; { procedure search_array } { ------------------------------------------------------------ } FUNCTION count_characters(VAR in_string : STRING; the_character : CHAR) : INTEGER; { this function returns an integer equal to the number of times the_character appears in the 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 } FOR J := 1 TO MAX DO { initialise values in array } WITH account_array_one[J] DO BEGIN name := ' '; { initialise variable to "blank" } address := ' '; number := ' '; balance := 0; { initialise variable to "zero" } END; account_array_one[15].name := 'John Smith'; account_array_one[15].address := '15 Hill St'; account_array_one[15].number := '12345'; account_array_one[15].balance := 213.23; WRITE('Enter the customer''s name : '); READLN(customer_name); WRITE('Enter a character : '); READLN(character); WRITE('The number of ',character,'''s in '); WRITE(customer_name,' is '); WRITELN(count_characters(customer_name,character):0); search_array(found, { procedure call } an_index, customer_name, account_array_one); IF found THEN { read as "IF (found is true) THEN" } BEGIN WRITELN('The customer details are:'); WRITE(' Name : '); WRITELN(account_array_one[an_index].name); WRITE(' Address : '); WRITELN(account_array_one[an_index].address); WRITE(' Number : '); WRITELN(account_array_one[an_index].number); WRITE(' Balance : '); WRITELN(account_array_one[an_index].balance:6:4); END ELSE WRITELN('That name is not in the data-base'); END. { program demo_calls }

Procedure calls

When you call your own procedures, it is important that the parameters in the procedure call are in the same order as they appear in the procedure definition. There must also be the same number of actual parameters as formal parameters. The parameters in the call must be separated by commas. Procedures can also be called from within other procedures, but a procedure can only be called by another procedure which is defined lower down in the source code. It is also possible for procedures to call themselves, this is called

Date: 2016-03-03; view: 498

<== previous page | next page ==>
Syntax of the FUNCTION heading | Useful Functions And Procedures
doclecture.net - lectures - 2014-2018 year. Copyright infringement or personal data (0.003 sec.)