home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Passing Values to Procedures

The procedure “DrawLine” is used to draw a line of a specific length (20), which may not be useful for any other application. In the following program the procedure is modified to draw a line whose length varies according to the length of the displayed text. When you run the program it asks you to enter a sentence, then displays the sentence between two lines of the same length as that sentence. Try the program first and then read the discussion.

{ ----------------------- figure 7-2 ---------------------------- }
PROGRAM Procedures2(OUTPUT);
VAR
 Len      :INTEGER;
 TestSentence  :STRING;
{ ------------ Beginning of Procedure ------------ }
PROCEDURE DrawLine(LineLength:INTEGER);
CONST
 Dash = '-';
VAR
 Counter:INTEGER;
BEGIN
 FOR Counter:= 1 TO LineLength DO
  WRITE(Dash);
 WRITELN
END;
{ --------------- End of Procedure ---------------- }
{ ----------------- Main program ------------------ }
BEGIN
 WRITE('Please enter a sentence: ');
 READLN(TestSentence);
 Len:= LENGTH(TestSentence);
 WRITELN;
 DrawLine(Len);
 WRITELN(TestSentence);
 Drawline(Len)
END.

Sample run:

Please enter a sentence: Learn Pascal in Three Days
-------------------
Learn Pascal in Three Days
-------------------

Instead of defining the number of dashes as a constant, the length of the sentence is declared in the main program as a variable “Len.” After the sentence is entered, its length is calculated and passed to the procedure as a parameter. The procedure call in this case becomes:

    DrawLine(Len);

The procedure header must also include a receiver parameter:

    PROCEDURE DrawLine(LineLength:INTEGER);

Between the parentheses comes the parameter “LineLength,” followed by a colon, followed by the type of the parameter (INTEGER).

When the procedure is invoked, the value of the variable “Len” (from the main program) is passed to the procedure and assigned to the variable “LineLength,” where it is used in processing. The variable “Len” is called the actual parameter, and the variable “LineLength” is called the formal parameter. After the procedure has been executed, the control is transferred back to the main program, and execution resumes at the next statement following the procedure call. Except during the procedure execution, the value of the formal parameter is undefined.

You may use literal values as actual parameters to call the procedure, such as:

    DrawLine(30);

This call results in the drawing of a line 30 characters long.


Note:  Functions and procedures can also be passed as parameters, but many implementations forbid this.

When a value is used as a parameter, it is said that the parameter is passed by value; if the parameter is a variable, it is said to be passed by reference.

Drill 7-1

Modify the last program so that you can pass to the procedure the type of line character (“-” or “*” etc.), and have the output displayed in the middle of the line (assume that the line is 80 characters wide). This is a sample run of the required program:

    Please enter a sentence: Learn C in Three Days
    Please enter the line character: *
                     *********************
                     Learn C in Three Days
                     *********************

A procedure call may contain more than one parameter, like this:

    Process(A, B, C);

The number of actual parameters in the procedure call must be the same as the number of formal parameters, which means that the procedure header may look something like this:

    PROCEDURE Process(X, Y:INTEGER; Z:REAL);

The variables “A” and “B” in the calling program must be of the type INTEGER as they correspond to “X” and “Y” respectively, while the variable “C” must be of the type REAL as it corresponds to “Z.” Note the semicolon that separates the declarations in the procedure header.

In brief, the actual and formal parameters must match in number, type, and position.

Passing Back Values from Procedures

A procedure may be used to change the value of a variable and pass it back to the calling program. In such a case, the formal parameters must be preceded by the word VAR. Consider the case of a procedure that receives the value of two variables and returns the cube of each. The procedure header might look something like this:

    PROCEDURE CubeThem(VAR X, Y:REAL);

You can only pass parameters to this procedure by reference:

    CubeThem(A, B);

The values of “A” and “B” will be passed to the procedure, substituted for “X” and “Y” respectively, cubed, and sent back to the calling program. It is illegal in this case to use literal values or expressions as actual parameters.

When formal parameters are preceded by the word VAR they are called variable parameters; otherwise they are value parameters.

The general form of the procedure header is:

    PROCEDURE name;
    or
    PROCEDURE procedure-name(formal-parameter-list);

The general form of a procedure call is:

    procedure-name;
    or
    procedure-name(actual-parameter-list);

The following program is an example of using both types of formal parameters. It demonstrates the same logic as the “PowerOperator” program (figure 7-2) does but uses a procedure to receive the base and the power and send back the result.

{ ----------------------- figure 7-3 --------------------------- }
PROGRAM VarParms(INPUT,OUTPUT);
VAR
 a, b, c:REAL;
{ ------------ Procedure Definition -------------- }
PROCEDURE PowerOperator(X, Y:REAL; VAR Z:REAL);
BEGIN
  Z:= EXP(LN(X)*Y)
END;
{ ---------------- Main Program ------------------ }
BEGIN
 WRITE('Enter the base and the exponent separated by a space:');
 READLN(a,b);
 PowerOperator(a, b, c);
 WRITELN('The value of ',a:0:2,' raised to the power of ',b:0:2,' is ',c:0:2)
END.

Sample run:

Enter the base and the exponent separated by a space:2 5
The value of 2.00 raised to the power of 5.00 is 32.00

Notice in the procedure that “X” and “Y” were declared as value parameters because they only receive values from the calling program, while “Z” was declared as a variable parameter because it sends back the result.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.