11.3 Structured Programming (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Explain where in the construction of an algorithm it is appropriate to use a function. Consider the benefits and potential drawbacks of using functions.
Functions are a fundamental and highly appropriate tool in algorithm design, particularly when dealing with code reuse, modularity, and readability. They are most beneficial when a specific sequence of instructions is needed repeatedly within an algorithm. Instead of repeating the same code block multiple times, a function encapsulates that block into a named unit. This offers several advantages:
- Code Reusability: A function can be called multiple times from different parts of the algorithm, avoiding redundancy. This makes the code more concise and easier to maintain.
- Modularity: Functions break down a complex algorithm into smaller, more manageable parts. Each function performs a specific task, making the overall algorithm easier to understand and debug.
- Readability: Using descriptive function names improves the clarity of the code, making it easier for others (and yourself) to understand the algorithm's purpose.
- Abstraction: Functions hide the internal implementation details of a task. The caller only needs to know what the function does, not how it does it. This simplifies the overall algorithm design.
However, there are potential drawbacks:
- Overhead: Calling a function involves some overhead (e.g., passing arguments, returning values), which can impact performance, especially for very simple operations.
- Complexity: Excessive use of functions can make the algorithm harder to follow if the functions are poorly designed or too complex.
Therefore, functions are most appropriate when a logical unit of work is performed repeatedly or when the task is complex enough to benefit from separation of concerns. For simple, one-off operations, inline code might be more efficient.
2.
Define what a procedure is in programming and explain the benefits of using procedures (also known as functions or subroutines). Provide a simple example of a procedure in any programming language of your choice.
Definition: A procedure (also known as a function or subroutine) is a named block of code that performs a specific task. It can accept input values (parameters) and may return a value as output. Procedures promote code reusability and modularity.
Benefits of using procedures:
- Code Reusability: A procedure can be called multiple times from different parts of the program, avoiding code duplication.
- Modularity: Procedures break down a large program into smaller, more manageable units, making the code easier to understand, debug, and maintain.
- Improved Readability: Using descriptive procedure names enhances the readability of the code.
- Abstraction: Procedures hide the implementation details of a task, allowing other parts of the program to use the procedure without needing to know how it works internally.
Example (Python):
def greet(name):
"""This procedure greets the person passed in as a parameter."""
print("Hello, " + name + "!")
greet("Alice")
greet("Bob")
In this example, greet
is a procedure that takes a name as input and prints a greeting. It can be called multiple times with different names.
3.
A student has written the following code in a programming language (you do not need to identify the specific language). The code is intended to calculate the sum of the first 'n' natural numbers. Identify the potential problems with this code in terms of its design and explain how a procedure could be used to improve it. Provide a brief example of how the improved code might look.
sum = 0
for i = 1 to n
sum = sum + i
print sum
Potential Problems:
- Lack of Reusability: The code is specific to calculating the sum of the first 'n' natural numbers. If you need to calculate the sum of a different set of numbers, you'd have to rewrite the code.
- Poor Modularity: The code is not broken down into logical units. This makes it harder to understand and maintain.
- Limited Readability: While relatively simple, the code lacks a descriptive name and doesn't clearly communicate its purpose beyond the calculation itself.
How a procedure could improve it: A procedure could encapsulate the entire calculation logic, making it reusable and more modular. The procedure would accept 'n' as a parameter and perform the summation. This would improve readability and allow the code to be easily used in other parts of a program.
Example of improved code (Conceptual - language agnostic):
PROCEDURE CalculateSum(n)
sum = 0
FOR i = 1 TO n
sum = sum + i
END FOR
RETURN sum
END PROCEDURE
BEGIN
result = CalculateSum(10) // Example: Calculate the sum of the first 10 natural numbers
PRINT result
END
This improved code defines a procedure CalculateSum
that takes 'n' as input and returns the sum. The main part of the program then calls this procedure with the desired value of 'n'.