|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
7-4 FunctionsA 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.
7-5 Tips on the Scope of VariablesThe 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:
|
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. |