3.13 Developing Procedures

8 阅读1分钟

1. Exam Points

  • Develop procedures and call procedures
  • Benefits of procedure abstraction
  • Use parameters to generalize functionality
  • Extract shared features to generalize functionality
  • Note:
    • Decide whether a procedure returns a value, if it does, use the RETURN statement in the procedure.
      image.png

2. Knowledge Points

(1) Procedural Abstraction (The Process of Creating Procedures)

  • Procedural abstraction: provides a name for a process and allows a procedure to be used only knowing what it does, not how it does it.
  • Example: Create a procedure(reuse) to compute the sum of all the elements in a given list.

    image.png
  • Procedural abstraction allows a solution to a large problem to be based on the solutions of smaller subproblems, therefore, creating procedures to solve each subproblem.
  • Example: create procedures for different tasks.

    image.png
  • A procedural abstraction may extract shared features to generalize functionality instead of duplicating code.
  • The subdivision of a computer program into separate subprograms is called modularity (模块化/性).
  • This allows for program code reuse, which helps manage complexity.
  • Using procedural abstraction helps improve code readability.
  • Programmers break down problems into smaller and more manageable pieces.
  • Procedures allow programmers to draw upon existing code that has already been tested, allowing them to write programs more quickly and with more confidence and less errors.
  • Summary:
    • modularize a big problem into more managable sub problems, helps manage complexity.
    • improves code reusability, reducing duplicate code.
    • code well structured, more readable and easier to maintain.
  • Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.

(2) Develop and Call Procedures

  • Develop and call a procedure:
    image.png
  • Example: Develop a procedure
    image.png
  • Example: Call a procedure
    • sum1 ← sum(aList)
    • sum2 ← sum(bList)

3. Exercises