Subprograms - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Subprograms

Description:

Subprograms facilitate the translation of the top-down algorithm to the final ... A side effect occurs when you unintentionally assign a new value to a global ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Subprograms


1
Subprograms
  • A subprogram is a part of the program that has
    all the elements of the program itself.
  • Subprograms facilitate the translation of the
    top-down algorithm to the final program because
    they allow us to concentrate on one task at a
    time.
  • Subprograms are called or activated from the
    Main Program

2
Subprograms Procedures
  • For now we will look at a type of subprogram
    called a procedure.
  • A Pascal procedure is similar in construction to
    a program. It can contain its own VAR
    declaration and/or CONST definition, and it
    contains its own statement part which begins with
    BEGIN and ends with END

3
Procedures (cont)
  • Differences between the construction of a program
    and a procedure
  • The reserved word PROCEDURE replaces the reserved
    word PROGRAM.
  • The final END is followed by a semicolon instead
    of a period.

4
Advantages of using procedures
  • If you want to perform a task more than once in a
    program, you just reference the procedure each
    time. This avoids repeating the same code.
  • The main program can be written as a sequence of
    tasks which makes it easier for humans to
    understand.

5
Global vs. Local variables
  • Global identifiers are declared in the main
    program. These variables can be used in the main
    program as well as in the procedures that are
    called from the main program.
  • Local identifiers are declared in the procedures.
    These variables can only be used by the
    procedure in which they are declared.

6
It is not a good idea to use global variables in
your procedures
  • Doing so can cause side effects. A side effect
    occurs when you unintentionally assign a new
    value to a global variable within a procedure.
    When you return to the main program and use that
    variable, you will get an unexpected result.

7
What if we declare the same identifier in both
the main program and one of its procedures?
  • The compiler will establish two different memory
    locations for the variables. It is as if they
    had different identifiers.
  • One location is available in the main program.
  • The other is available only in the
    procedure. ie. It takes precedence inside the
    procedure.

8
Homework 5
  • Due November 2, 1999
  • It is homework 4 on Maratecks
    homepagehttp//cs.nyu.edu/courses/fall97/A22.00
    02/hw4.html

9
Next class midterm review
  • Please be prepared to ask me questions
  • You go over and be comfortable with
  • my old midterm
  • Maratecks old midterm
  • my program which converts chars to decimals using
    a FOR DO loop
  • nested loops
  • plus everything I listed last class
Write a Comment
User Comments (0)
About PowerShow.com