110 likes | 123 Views
This text discusses the concept of parameter sharing in procedures, the use of variable parameters, and the role of the exit statement. It also provides examples and considerations for using these concepts effectively.
E N D
Procedures: Parameters • If using global variables is a bad idea, what do you use when main needs to share variables with other procedures? • Answer: Parameters
Parameters PROGRAM Example; VAR A,B: integer; PROCEDURE Proc (Index1, Index2: integer); BEGIN … END; BEGIN {main} … Proc (A,B); END.
Parameters (con’t) • Formal (dummy) parameters: The parameter list in the PROCEDURE declaration. • PROCEDURE Proc (Index1,Index2: integer);Index1 and Index2 are the formal parameters • Actual parameters: The parameters contained in the activating statement. • Proc (A,B);A and B are the actual parameters
Value parameters • Value parameters should be thought of as one way parameters. Your activating statement sends the values to the procedure. Once the procedure finishes, the formal parameter value is lost. Therefore, if it changes, there will be no side effect to the main program.
Variable Parameters • What if you want the value of the actual parameters to change after the procedure finishes?Answer: Use Variable parameters
Variable parameters (con’t) PROGRAM Example; VAR A,B: integer; PROCEDURE Proc (VAR Index, Index2: integer); BEGIN … END; BEGIN {main} … Proc (A,B); END.
How does the compiler deal with each type of parameter? • For every variable parameter, the formal and actual parameters share the same memory location. What is passed is a ‘reference’ to a memory location. • For every value parameter, the formal and the actual parameters are each given a separate memory location.
Parameters (con’t) • One procedure can contain both types of parameters: • PROCEDURE Sample (VAR A: integer; B: char); • You must rewrite VAR for every variable type if you want variable parameters: • PROCEDURE Sample2 (VAR A: integer; VAR B:char);
Exit Statement • What it you want a procedure to end before the last line of the procedure? • You can do so by placing the statement ‘EXIT’ anywhere in the procedure. • Reasons why you need this statement will become more apparent in the next chapter.
A few final comments on procedures • What should go into a procedure? • “Bite size chunks” • complete operations • For now, a procedure can only call another procedure which appears “before” itself. • Only use variable parameters when it is necessary.
Homework Due March 10, 2000 • It is homework #4 on Marateck’s homepage:http://cs.nyu.edu/courses/fall97/A22.0002/hw4.html