Functions and Procedures (OCR A-Level Computer Science): Revision Notes
📚 Revision Notes
Functions and Procedures
Overview
Functions and procedures are key components of modular programming. They allow developers to organise code into smaller, reusable blocks. Although both serve similar purposes, there are important differences in how they operate and are used.
Understanding how to define, use, and trace functions and procedures is essential for writing efficient, modular, and maintainable code.
What are Functions?
- Definition: A function is a block of code that performs a specific task and returns a value.
- Purpose: Used when the result of the operation needs to be used elsewhere in the programme.
lightbulbExample
Example in Pseudocode:
FUNCTION calculateArea(radius)
RETURN 3.14 * radius * radius
END FUNCTION
lightbulbExample
Example in Python:
def calculate_area(radius):
return 3.14 * radius * radius
What are Procedures?
- Definition: A procedure is a block of code that performs a specific task but does not return a value.
- Purpose: Used when the task modifies data, interacts with the user or produces an output without needing to return a value.
lightbulbExample
Example in Pseudocode:
PROCEDURE displayMessage()
PRINT "Hello, World!"
END PROCEDURE
lightbulbExample
Example in Python:
def display_message():
print("Hello, World!")
Differences Between Functions and Procedures
| Aspect | Functions | Procedures |
|---|---|---|
| Return Value | Returns a value using RETURN. | Does not return a value. |
| Usage | Used to perform calculations or tasks that need a result. | Used to execute tasks like displaying output. |
| Integration | Often used within expressions or other functions. | Typically called as standalone statements. |
| Example Usage | result = calculateArea(5) | displayMessage() |
Using Functions and Procedures Together
Functions and procedures can work together to build modular, efficient programmes.
infoNote
Example: Student Grade System
Function to Calculate Average:
FUNCTION calculateAverage(marks)
total = 0
FOR EACH mark IN marks
total = total + mark
ENDFOR
RETURN total / LENGTH(marks)
END FUNCTION
Procedure to Display Result:
PROCEDURE displayResult(name, average)
PRINT "Student:", name
PRINT "Average Mark:", average
END PROCEDURE
Main Programme:
MAIN
marks = [85, 90, 78]
average = calculateAverage(marks)
displayResult("Alice", average)
END MAIN
Tracing Code with Functions and Procedures
infoNote
Example with Trace Table:
Pseudocode:
FUNCTION square(x)
RETURN x * x
END FUNCTION
PROCEDURE printSquare(value)
result = square(value)
PRINT result
END PROCEDURE
MAIN
printSquare(3)
END MAIN
Trace Table:
| Step | Function/Procedure | Input | Output | Result |
|---|---|---|---|---|
| 1 | square(3) | 3 | 9 | 9 |
| 2 | printSquare(3) | 3 | Prints 9 | - |
Benefits of Using Functions and Procedures
- Code Reusability: Functions and procedures can be called multiple times, reducing code duplication.
- Improved Readability: By breaking the programme into smaller sections, modular code is easier to understand and maintain.
- Simplified Testing and Debugging: Individual functions and procedures can be tested independently.
- Collaborative Development: Different team members can work on different functions or procedures.
Practical Applications of Functions and Procedures
- Mathematical Calculations: Functions like
calculateArea()orfindMaximum()can handle repetitive calculations. - User Interaction: Procedures like
displayMenu()orprintErrorMessage()manage user interactions. - File Handling: Procedures can open, read, write, or close files while functions return specific data.
Note Summary
infoNote
Common Mistakes
- Confusing Functions and Procedures: Attempting to use a
RETURNstatement in a procedure or failing to return a value from a function. - Incorrectly Handling Return Values: Forgetting to store or use the result of a function.
- Scope Errors: Misusing variables outside their intended scope.
- Redundant Code: Failing to use functions or procedures where appropriate, leading to repetitive code.
infoNote
Key Takeaways
- Functions return a value and are used for tasks that produce a result.
- Procedures do not return a value and are used for tasks that perform an action, such as printing.
- Combining functions and procedures helps create modular code, improving readability, reusability, and maintainability.
- Understanding and correctly using these constructs is crucial for writing efficient and organised programmes.