Structured programming means writing code in a clear, logical way using sequence, selection, and iteration — and breaking large problems into smaller, manageable subproblems. This makes programs easier to write, test, and maintain.
Decomposition is the process of breaking a complex problem into smaller sub-problems that can each be solved independently. Each sub-problem becomes a subroutine.
| Benefit | Explanation |
|---|---|
| Avoid repetition | Write the code once and call it multiple times — no copy-paste errors |
| Easier testing | Each subroutine can be tested independently |
| Readability | Main program is shorter and easier to understand |
| Teamwork | Different programmers can write different subroutines |
| Reusability | Subroutines can be used in other programs |
A procedure performs a task but does NOT return a value. It is called using the CALL keyword.
PROCEDURE Greet(name : STRING)
OUTPUT "Hello, " & name & "!"
ENDPROCEDURE
CALL Greet("Alice") // outputs: Hello, Alice!
PROCEDURE PrintLine()
OUTPUT "-------------------"
ENDPROCEDURE
CALL PrintLine()
OUTPUT "Student Report"
CALL PrintLine()
A function performs a calculation and returns a value to where it was called. The return type is declared after RETURNS. You use the function in an expression (not with CALL).
FUNCTION Square(n : INTEGER) RETURNS INTEGER
RETURN n * n
ENDFUNCTION
result ← Square(5) // result = 25
OUTPUT Square(4) + 1 // outputs: 17
FUNCTION Max(a : INTEGER, b : INTEGER) RETURNS INTEGER
IF a > b THEN
RETURN a
ELSE
RETURN b
ENDIF
ENDFUNCTION
largest ← Max(12, 7) // largest = 12
| Feature | Procedure | Function |
|---|---|---|
| Returns a value? | No | Yes (declared with RETURNS) |
| How to call | CALL name(args) | name(args) in an expression |
| Keyword to end | ENDPROCEDURE | ENDFUNCTION |
| Main use | Perform an action (e.g., output, write to file) | Compute and return a result |
| RETURN statement | Not used | Required — returns the value |
Parameters are values passed into a subroutine. They are listed in brackets after the subroutine name with their data types: name : DATATYPE. Multiple parameters are separated by commas.
PROCEDURE PrintMultiple(message : STRING, times : INTEGER)
DECLARE i : INTEGER
FOR i ← 1 TO times
OUTPUT message
NEXT i
ENDPROCEDURE
CALL PrintMultiple("Hello!", 3)
// outputs: Hello! / Hello! / Hello!
// Main program — uses subroutines for each section
DECLARE name : STRING
DECLARE score : INTEGER
CALL GetStudentData(name, score)
CALL PrintReport(name, score, Grade(score))
// Subroutines:
PROCEDURE GetStudentData(n : STRING, s : INTEGER)
OUTPUT "Enter student name:"
INPUT n
OUTPUT "Enter score (0-100):"
INPUT s
ENDPROCEDURE
FUNCTION Grade(s : INTEGER) RETURNS STRING
IF s >= 70 THEN
RETURN "A"
ELSE IF s >= 60 THEN
RETURN "B"
ELSE IF s >= 50 THEN
RETURN "C"
ELSE
RETURN "Fail"
ENDIF
ENDFUNCTION
PROCEDURE PrintReport(n : STRING, s : INTEGER, g : STRING)
OUTPUT "Student: " & n
OUTPUT "Score: " & s
OUTPUT "Grade: " & g
ENDPROCEDURE
3 questions · 11 marks
| Term | Definition |
|---|
10 minutes · mixed marks