Benefits of Sub-Procedures
| Advantage | Practical benefit |
|---|---|
| Modularity | Components can be built independently |
| Reusability | Same routine can be reused across features |
| Testability | Each routine can be unit tested in isolation |
| Maintainability | Fix once in one procedure, benefit everywhere |
| Readability | Smaller named routines make code easier to follow |
| Team collaboration | Different developers can own different procedures |
The Interface of a Sub-Procedure
| Sub-procedure | Parameters (inputs) | Returns (output) |
|---|---|---|
| getISBN | bookTitle | The ISBN of the matching book |
| checkAvailability | ISBN | True if a copy is in stock, otherwise false |
| confirmReservation | userID, ISBN, location | Nothing: it is a procedure that saves the reservation |
Agreeing each interface before any code is written lets different programmers write and test their sub-procedures separately, because each one knows what the others will supply and expect.
// Sub-procedure 1: Get user name
function getName():
if isSignedIn() then
return currentUser.name
else
return null
end if
end function
// Sub-procedure 2: Get book ISBN
function getISBN(bookTitle):
book = database.findBook(bookTitle)
return book.ISBN
end function
// Sub-procedure 3: Check availability
function checkAvailability(ISBN):
return database.getStock(ISBN) > 0
end function
// Sub-procedure 4: Select location
function selectLocation():
return displayLocationMenu()
end function
// Sub-procedure 5: Confirm reservation
procedure confirmReservation(userID, ISBN, location):
database.createReservation(userID, ISBN, location)
end procedure
// Main procedure combining all sub-procedures
procedure main():
name = getName()
ISBN = getISBN(input("Book title: "))
if checkAvailability(ISBN) then
location = selectLocation()
confirmReservation(name, ISBN, location)
end if
end procedure
• Be able to decompose a problem into components
• Identify appropriate sub-procedures for given scenarios
• Understand the order/sequence of steps
• Recognise dependencies between procedures
• Explain benefits of decomposition (modularity, testing, teamwork)
• Write pseudocode for sub-procedures with clear inputs/outputs