1 / 12

Midterm #1

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.

mccoolc
Télécharger la présentation

Midterm #1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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%

  2. 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

  3. Parameters PROGRAM Example; VAR A,B: integer; PROCEDURE Proc (Index1, Index2: integer); BEGIN … END; BEGIN {main} … Proc (A,B); END.

  4. 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

  5. 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.

  6. Variable Parameters • What if you want the value of the actual parameters to change after the procedure finishes?Answer: Use Variable parameters

  7. Variable parameters (con’t) PROGRAM Example; VAR A,B: integer; PROCEDURE Proc (VAR Index, Index2: integer); BEGIN … END; BEGIN {main} … Proc (A,B); END.

  8. 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.

  9. 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);

  10. 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.

  11. 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.

  12. Skip section 6.6 Scope of Identifiers

More Related