Sub-Procedures (OCR A-Level Computer Science): Revision Notes
Sub-Procedures
Overview
Sub-procedures are smaller, self-contained units of code within a programme that perform specific tasks. They are often referred to as functions or methods depending on the programming language. Sub-procedures help to break down complex problems into manageable parts, improving the readability, reusability, and maintainability of the code.
Understanding how to identify, design, and implement sub-procedures is essential for writing efficient and organised programmes.
What are Sub-Procedures?
- Definition: A sub-procedure is a reusable block of code that performs a specific task within a larger programme.
- Types of Sub-Procedures:
- Functions: Return a value after execution.
- Procedures/Methods: Perform a task but do not necessarily return a value.
Purpose of Sub-Procedures
- Modularity: Breaks the programme into smaller, manageable sections.
- Reusability: Allows code to be reused in multiple places.
- Readability: Makes the main programme easier to understand by delegating tasks.
- Ease of Maintenance: Simplifies debugging and updating since changes can be made in one place.
Identifying Sub-Procedures in a Problem
To identify sub-procedures:
- Analyse the Problem: Break down the problem into smaller tasks or steps.
- Identify Repeated Tasks: Tasks that occur multiple times can be extracted as sub-procedures.
- Look for Logical Groupings: Group related tasks together.
- Define Input and Output: Determine what data the sub-procedure needs to work on (inputs) and what it should return or affect (outputs).
Example Scenarios
Scenario 1: Online Shopping System
- Problem: Implement an online shopping cart.
- Identified Sub-Procedures:
- addToCart(item, quantity): Adds an item to the shopping cart.
- calculateTotal(cart): Calculates the total cost of items in the cart.
- applyDiscount(cart, discountCode): Applies a discount to the total.
- checkout(cart, paymentDetails): Processes the payment.
Scenario 2: Student Grading System
- Problem: Develop a programme to calculate and display student grades.
- Identified Sub-Procedures:
- inputGrades(): Collects grades from the user.
- calculateAverage(grades): Computes the average grade.
- determineGrade(average): Determines the final grade based on the average.
- displayResults(grade): Displays the student's final grade.
Writing Sub-Procedures
Pseudocode Example: Student Grading System
PROCEDURE inputGrades()
DECLARE grades AS ARRAY
FOR i = 1 TO 5
OUTPUT "Enter grade:"
INPUT grade
APPEND grade TO grades
ENDFOR
RETURN grades
ENDPROCEDURE
PROCEDURE calculateAverage(grades)
DECLARE total AS INTEGER = 0
FOR EACH grade IN grades
total = total + grade
ENDFOR
RETURN total / LENGTH(grades)
ENDPROCEDURE
PROCEDURE determineGrade(average)
IF average >= 70 THEN
RETURN "A"
ELSEIF average >= 60 THEN
RETURN "B"
ELSEIF average >= 50 THEN
RETURN "C"
ELSE
RETURN "F"
ENDIF
ENDPROCEDURE
PROCEDURE displayResults(grade)
OUTPUT "Final Grade: ", grade
ENDPROCEDURE
Flowchart Example: calculateAverage(grades)
- Start.
- Initialise total to 0.
- Loop through each grade in grades.
- Add grade to total.
- After the loop, calculate average = total / number of grades.
- Return average.
- End.
Using Parameters in Sub-Procedures
Parameters are used to pass data into sub-procedures, making them more flexible and reusable.
Example: In the procedure calculateAverage(grades), grades is a parameter that allows the procedure to work with any set of grades.
Structure Diagrams
A structure diagram visually represents the relationship between sub-procedures in a programme. It shows the hierarchical organisation of tasks.
Example: Structure Diagram for Student Grading System
Main Programme
├── inputGrades()
├── calculateAverage(grades)
├── determineGrade(average)
└── displayResults(grade)
Benefits of Sub-Procedures
- Improved Modularity: Each sub-procedure focuses on a single task, simplifying programme design.
- Code Reusability: Sub-procedures can be used in different parts of the programme or other projects.
- Simplified Debugging: Errors can be isolated within specific sub-procedures.
- Ease of Testing: Individual sub-procedures can be tested independently.
Note Summary
Common Mistakes
- Overcomplicating Sub-Procedures: Each sub-procedure should perform a single, well-defined task.
- Incorrect Use of Parameters: Failing to pass the correct data or using too many parameters can lead to confusion and errors.
- Poor Naming Conventions: Sub-procedure names should reflect their purpose.
Key Takeaways
- Sub-procedures help break down complex problems into manageable tasks, improving code readability, reusability, and maintainability.
- They can be represented using pseudocode, flowcharts, or structure diagrams.
- Proper use of parameters enhances the flexibility and utility of sub-procedures.
- Effective design and implementation of sub-procedures lead to more organised and efficient programmes.