Explain where in the construction of an algorithm it would be appropriate to use a procedure

🧩 11.3 Structured Programming – When to Use a Procedure

What is a Procedure?

A procedure (or subroutine) is a named block of code that performs a specific task. Think of it as a recipe: you give it some ingredients (parameters), it does its cooking (code), and then it returns a finished dish (output or side‑effects).

Algorithm Construction Stages

  1. Problem Analysis – Understand the problem, identify inputs, outputs, and constraints.
  2. Algorithm Design – Decide on the steps and logic needed.
  3. Pseudocode Writing – Translate the design into readable pseudo‑code.
  4. Implementation – Write the actual code in a programming language.
  5. Testing & Debugging – Verify correctness and fix errors.

When to Insert a Procedure?

  • Repetition – If you find yourself writing the same block of code more than once, extract it into a procedure. Example: calculating the area of a circle in multiple places.
  • Complexity Reduction – Break a long, tangled block into smaller, manageable parts. Each part becomes a procedure.
  • Reusability – A procedure can be reused across different parts of the program or even in other projects.
  • Testing & Debugging – Smaller units are easier to test in isolation.

Illustrative Example – A Simple Calculator

Step Description Procedure Used?
1 Read two numbers from the user. No – simple input.
2 Ask which operation (+, –, ×, ÷) to perform. No – simple input.
3 Compute the result. Yescalculate(a, b, op) procedure handles all operations.
4 Display the result. No – simple output.

In the example above, the calculate procedure is called only once, but its logic is reused for each operation. If we had to perform the same calculation in several different parts of a larger program (e.g., a financial app), we could call calculate from each place without duplicating code.

Exam Tip Box

Tip: When answering “Where would you use a procedure?” always mention the benefits – modularity, readability, reusability, and easier testing. Use the phrase: “I would use a procedure when … because …”

Remember to illustrate with a short example (like the calculator) and show the procedure call in pseudocode.

Quick Checklist

  • Is the code block repeated elsewhere? → Extract.
  • Is the block long or complex? → Split into a procedure.
  • Will you need to test this block independently? → Make it a procedure.
  • Can the procedure be reused in other programs? → Yes, create it.

Revision

Log in to practice.

2 views 0 suggestions