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


7-4 Functions

A function is a subprogram that returns a value, which is then assigned to the function name in the calling program. Like predefined functions, user-defined functions have one or more parameters. The function definition comes in the subprogram section of the declaration part and includes a header, a declaration part, and statements. Look at this header of a function that returns the average of three numbers:

    FUNCTION Avg(X, Y, Z:REAL):REAL;

The header is similar to the procedure header except that the type of the return value follows the function header (:REAL). You can call this function using statements like these:

    D:= Avg(A, B, C);
    WRITELN(Avg(F, G, H):2:2);
    WRITELN(Avg(94, 33.5, 45*1.2):2:2);

As you can see, the parameter may be a literal constant, an expression, or a variable.

The function header takes the following form:

    FUNCTION function-name(formal-parameter-list) :return-type;

In this program the function “Avg” is demonstrated.

{ ----------------------- figure 7-5 --------------------------- }
PROGRAM Functions1(INPUT, OUTPUT);
VAR
 A, B, C:REAL;
{ ------------ Beginning of Function ------------- }
FUNCTION Avg(X, Y, Z:REAL):REAL;
BEGIN
 AVG:= (X + Y + Z) / 3
END;
{ --------------- End of Function ---------------- }
{ ----------------- Main program ----------------- }
BEGIN
 WRITE('Enter three numbers: ');
 READLN(A, B, C);
 WRITELN('The average is= ', Avg(A, B, C):0:2)
END.

Sample run:

Enter three numbers: 2 3 8
The average is= 4.33

Like procedures, functions are independent subprograms. All parameters, variables, and constants declared within the function body are local to it and are invisible to other program units. In a function subprogram, the function must be assigned a value.


TIP:  In a function subprogram, the function name cannot be treated like a variable; i.e., it may not be involved in expressions. It may only be assigned a value.

Drill 7-2

Write a function to return the maximum number in a one-dimensional array and include the function in a program. You may use any procedures you wrote before to build the program.

7-5 Tips on the Scope of Variables

The following program frame consists of three program units, procedure “Kid1,” procedure “Kid2,” and the main program “Parent.” According to the rules of variable scope, any variable declared in “Parent” (global variable) is accessible to both “Kid1” and “Kid2” unless it is redeclared locally in either of them. On the other hand, any local variable declared in “Kid1” is hidden from both “Parent” and “Kid2.” The same thing applies for “Kid2” variables. If you consider the main program as a parent and the subprograms as kids, it then follows that whatever belongs to the parent belongs to the kids, but the opposite is not valid. In other words, the kids inherit everything from the parent, but each one of them has his own property, which is not inherited by a parent or a sibling.

{ ----------------------- figure 7-6 ---------------------------- }
PROGRAM Parent;
     { ------------------- PROCEDURE KID1 ----------------------- }
     PROCEDURE Kid1(...);
     ...
     BEGIN
     ...
     END; { ----------------- END OF PROCEDURE KID1 -------------- }
     { ---------------- PROCEDURE KID2 ------------------ }
     PROCEDURE kid2(...);
     ...
     BEGIN
     ...
     END; { -------------- END OF PROCEDURE KID2 ------------- }
{ -------------------------- MAIN PROGRAM ------------------------- }
BEGIN
...
END.

Either of the two procedures may be called from the main program. The procedure “Kid1” may also be called from “Kid2” because it has already been defined, but the procedure “Kid2” cannot be called from “Kid1” because it has not yet been defined. There is a way to get around this restriction using a forward declaration by including the header of “Kid2,” followed by the keyword FORWARD, at the beginning of the program, like this:

    PROGRAM Parent;
    { Forward declaration of Kid2 }
           PROCEDURE Kid2(...); FORWARD;
    { Definition of Kid1 }
           PROCEDURE Kid1(...);
            ...
    { Definition of Kid2 }
           PROCEDURE kid2(...);
            ...
    { Main program }
            ...

Now take a look at the new program structure in the following figure. The procedure “GrandKid” is defined inside the procedure “Kid,” which means that “Kid” has become the parent of another subprogram. In such a case, any variable in “Kid” is global in “GrandKid,” and so are the variables of the “Parent” (unless any of them is redeclared in “GrandKid”). The local variables in “GrandKid,” however, are not accessible to either “Kid” or “Parent.”

{ ------------------ figure 7-7 ---------------------- }
PROGRAM Parent;
     { ------------------- PROCEDURE KID ------------------- }
     PROCEDURE Kid(...);
     ...
            { -------------- PROCEDURE GRANDKID -------------- }
            PROCEDURE GrandKid(...);
            BEGIN
            ...
            END; { ------- END OF PROCEDURE GRANDKID ------- }
     BEGIN
     ...
     END; { ------------- END OF PROCEDURE KID -------------- }
{ ------------------ MAIN PROGRAM -------------------- }
BEGIN
...
END.

To summarize:

1.  The scope of a variable is the program unit in which it is declared.
2.  A global variable is accessible in any program unit unless it is redeclared locally in that unit.
3.  A local variable is not accessible outside the program unit in which it is declared. It is, however, accessible to any subprogram defined within this program unit unless redeclared inside that sub-subprogram.
4.  Any subprogram can be called from any program unit as long as its definition (or its forward declaration) preceded the call.


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.