Passing Parameters in Pascal - Pascal programming

In, Out, In and Out, and Neither

Procedures are representations of independent algorithms and, as such, they present a great many different aspects that cannot be conveyed by the study of only a few examples. In this section, we will show many examples of small procedures, to illustrate the great diversity of details. In Pascal, data are passed from the actual parameters of a call statement to the formal parameters of the called procedure, either by value or by reference. Input parameters are passed by value, while output and input-output parameters are passed by reference. It is very important that this distinction be absolutely clear in your mind for two reasons:

  • Expressions may only be passed to input parameters, while the actual parameters corresponding to output and input-output parameters must be variables.
  • Variables passed to input parameters cannot have their values changed by the procedure, whereas variables passed to output and input-output parameters can, and generally do, have their values changed by the procedure.

To make this distinction clear, appropriate comments can be put in the procedure headers. The comments { input }, { output } and { through } can be put at the right of the corresponding parameters, as we have done in our previous examples and will do in this section. You are likely to find that some of the procedures shown in the following programs can be used in your programming. You can copy their definitions directly into your program, or you can use them as models on which to base procedures tailored to your particular needs. They could also be put into a Library and be available to allprogram.

BigChange

The first example, program BigChange in Figure is an enlarged version of the program Change shown earlier in Figure.

Program BigChange

The BigChange program makes use of a number of different procedures:

Spellout(Number)—Number is an input parameter. This procedure outputs small integer values, passed in Number, in words instead of digits. Since Number is an input parameter, it is passed by value. Only the numbers zero through nine are output as words; outside that range, numerical characters are used.

Enterpos(PositiveNumber)—PositiveNumber is an output parameter.

This procedure reads in an INTEGER called InputValue, tests whether it is positive, and if not keeps prompting and reading in numbers. When the input value is positive then it is assigned to whatever variable PositiveNumber corresponds to in the calling program. The procedure can be used to enter positive values for variables such as Age, Height, IdNumber, etc. The VAR preceding the variable PositiveNumber indicates passing by reference, as does the comment { output } at its right. A second VAR precedes the declaration of the local variable InputValue.

Divide(Numerator, Denominator, Quotient, Remainder)— Numerator and Denominator are input parameters; Quotient and Remainder are output parameters. This procedure receives two values (Numerator and Denominator) and passes out two values (Quotient and Remainder). It has been discussed in detail and needs no further explanation. Following the declarations of these three procedures are the declarations of the variables that are used in the program’s body. They are listed close to the BEGIN because that is where they are used. Things that go together should be together. There are seven variables all of type INTEGER. Running this program for some typical data yields the following output:

Enter the cost: Enter a positive number 13 Enter the amount tendered: Enter a positive number 100 The change is: the number of quarters is three the number of dimes is one the number of nickels is zero the number of pennies is two

BigPay

Program BigPay

PROGRAM BigPay;

(* Compute the net pay and the deductions for a Number of employees, whose data is kept in input file Employee.Data. The results are given in a table and the total net pay is displayed *)

Our second example is a Pascal version of the BigPay algorithm. This version is shown in Figure, and has been written so that it obtains its data from a file whose layout is: Number of employees One data record showing hours worked and rate of pay for each employee. The actual contents of the file used with this example are the following.

10 45.5 11.25 40.0 6.50 37.5 10.25 43.0 11.50 40.0 4.50 42.4 14.50 40.0 6.75 40.0 8.50 52.0 4.50 28.5 4.50

These data correspond to ten employees with hours and rates of pay as show The body of the program, between the last BEGIN-END pair, first sets up the InputFile for reading, and reads the number of employees to be processed and sets that in EmployeeCount. It then initializes the variable to accumulate the total pay to zero, and outputs the header for the pay ledger that will be produced. It then executes a loop once for each employee. In the body of the loop, it prints the employee number, calls the procedure NetPay to calculate the employee's net pay and add it to the total. The program includes the following procedures:

GrossPay(Pay)

This procedure has a single output parameter, Pay, in which the calculated gross pay is set. Hours worked and Rate of pay for the employee are read from the InputFile and the Pay computed. Note that since InputFile is a global name—it is not passed as a parameter—it must be declared before the procedure is defined.

GetMiscDeductions(MiscDeductions)

This procedure has a single output parameter, MiscDeductions. In fact, this procedure is a stub —a temporary version of the procedure used during the development of the program. The details of the computation of the miscellaneous deductions have not been elaborated; it just returns a constant value of 10.75 for each employee, which is specified as a constant.

Deductions(Gross, Total)

This procedure has one input parameter, Gross, and one output parameter, Total. Note that the parameter Total is quite separate from the variable Total declared at the top of the program. There are two local variables, Tax and Misc.

NetPay(Amount)

This procedure has a single input-output parameter Amount to which the ActualPay is added. The result of running BigPay with the sample data in the input file is given in Figure. You may note that the net pay total is not exact (it is one cent too much); this is proof of the disadvantage of using REAL variables.

Results produced by BigPay

Emp Hours Rate Gross Tax Misc Actual Num Pay Pay 1 45.50 11.25 458.25 123.73 10.75 323.77 2 40.00 6.50 260.00 70.20 10.75 179.05 3 37.50 10.25 384.37 103.78 10.75 269.84 4 43.00 11.50 464.50 125.42 10.75 328.33 5 40.00 4.50 180.00 48.60 10.75 120.65 6 42.40 14.50 583.60 157.57 10.75 415.28 7 40.00 6.75 270.00 72.90 10.75 186.35 8 40.00 8.50 340.00 91.80 10.75 237.45 9 52.00 4.50 198.00 53.46 10.75 133.79 10 28.50 4.50 128.25 34.63 10.75 82.87 The total net pay is 2277.39

A Miscellany of procedures

Program MiscProcs1

Our next example is program MiscProcs1, shown in Figure, that embodies a mixed collection of procedures that serves to further illustrate some more forms of procedures.

Instructions()

This first procedure, simply displays some instructions to the user of the program. It involves no passing of parameters and has no local variables.

WriteStar15()

This procedure outputs a line of 15 asterisks and terminates the current output line. It also has no parameters, but has a local variable Count used to count the asterisks.

WriteStar(Number)

This is a more general version of WriteStar15 that has one input parameter Number, which specifies the number of asterisks to be output. It does not need to use a local variable since the value of the input parameter Number serves as a counter. Since input parameters are passed by copying their value, when the value of Number is changed in the procedure, it does not change the value of the corresponding actual parameter in the main program.

Decr(Value)

This procedure has a single input-output parameter Value, which it decrements by 1, i.e. it is similar to standard procedure Dec. It serves to illustrate the use of an input-output parameter, one whose value is passed in, modified and then passed out to the same variable in the calling program.

WriteChar(Symbol, Number)

outputs a line consisting of the Symbol character repeated Number times. It is a further generalization of WriteStar. The body of MiscProcs1 illustrates the calling of the above procedures in trivial ways. Notice especially that each call is a statement. The output obtained from running the program is:

************************ Enter percentages as whole numbers. End on a negative ------------------------

A Second Miscellany of procedures

Program MiscProcs2

A second miscellany of procedures, program MiscProcs2 shown in Figure, contains some procedures that are generally useful, either as such or as the basis from which particular procedures can be crafted.

SnapTrace()

Like Instructions seen above, this procedure also has no parameters and no local variables. It outputs the values of three global variables C, I and R. Note that these variables must be declared before this procedure is defined. Calls of this procedure could be inserted at various points in a program to test or debug it.

TemperatureFtoC(Fahrenheit, Celsius) is a simple temperature conversion procedure that has one input, Fahrenheit, and one output, Celsius, and no local variables. AreaCircle(Radius, Area) is another simple algebraic formula packaged as a procedure. Notice that Pi does not need to be declared since it is a standard function.

Maxi2(X, Y, Max)

has two inputs X and Y and an output Max. It applies only to integers.

Maxi3(A, B, C, Largest)

has three inputs A, B, C and one output Largest. It calls the previous Maxi2 procedure twice. Again, the body of this program illustrates the calling of these procedures in simple ways. The output obtained from running MiscProcs2 is given in Figure.

Results of program MiscProcs2 execution

212F is 100.00C Max of 3, 6, 5 = 6 Area of Circle of radius 1 is 3.14 Area of Circle of radius 2 is 12.57 Area of Circle of radius 3 is 28.27 Area of Circle of radius 4 is 50.27 Area of Circle of radius 5 is 78.54 First = # Second = 6 Third = 100.00

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Pascal programming Topics