Procedures: Parameters - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Procedures: Parameters

Description:

If using global variables is a bad idea, what do you use when main needs to ... Value parameters should be thought of as one way parameters. ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 12
Provided by: evank
Learn more at: https://cs.nyu.edu
Category:

less

Transcript and Presenter's Notes

Title: Procedures: Parameters


1
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

2
Parameters
  • PROGRAM Example
  • VAR A,B integer
  • PROCEDURE Proc (Index1, Index2 integer)
  • BEGIN
  • END
  • BEGIN main
  • Proc (A,B)
  • END.

3
Parameters (cont)
  • 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

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

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

6
Variable parameters (cont)
  • PROGRAM Example
  • VAR A,B integer
  • PROCEDURE Proc (VAR Index, Index2 integer)
  • BEGIN
  • END
  • BEGIN main
  • Proc (A,B)
  • END.

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

8
Parameters (cont)
  • 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
    Bchar)

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

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

11
Homework Due March 10, 2000
  • It is homework 4 on Maratecks
    homepagehttp//cs.nyu.edu/courses/fall97/A22.00
    02/hw4.html
Write a Comment
User Comments (0)
About PowerShow.com