120 likes | 139 Views
Learn the importance of parameters in procedures, including formal and actual parameters, value parameters, and variable parameters. Understand how the compiler deals with each type of parameter and the use of 'EXIT' statement.
E N D
Midterm #1 • 90-100 34 • 80-89 9 • 70-79 5 • 65-69 4 • 0-64 6 • Please see me if your score was below 70%
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.
Skip section 6.6 Scope of Identifiers