CS12: Subroutines
Understanding procedures, functions, parameters, return values, local vs global variables, and the benefits of structured programming.
Understanding procedures, functions, parameters, return values, local vs global variables, and the benefits of structured programming.
There are two types of subroutines:
PROCEDURE greet(name)
OUTPUT "Hello, " & name & "!"
ENDPROCEDURE
// Calling the procedure
greet("Alice") // Output: Hello, Alice!
greet("Bob") // Output: Hello, Bob!
PROCEDURE displayScore(name, score)
OUTPUT name & " scored " & STR(score) & " points"
ENDPROCEDURE
displayScore("Alice", 95) // Output: Alice scored 95 points
displayScore("Bob", 72) // Output: Bob scored 72 points
FUNCTION calculateArea(length, width)
area ← length * width
RETURN area
ENDFUNCTION
// Calling the function and storing the result
roomArea ← calculateArea(5, 4)
OUTPUT "Area = " & STR(roomArea) // Area = 20
FUNCTION circleArea(radius)
RETURN 3.14159 * radius * radius
ENDFUNCTION
// Using the function directly in output
OUTPUT circleArea(10) // 314.159
FUNCTION getGrade(score)
IF score >= 90 THEN
RETURN "A*"
ELSEIF score >= 80 THEN
RETURN "A"
ELSEIF score >= 70 THEN
RETURN "B"
ELSE
RETURN "C or below"
ENDIF
ENDFUNCTION
OUTPUT getGrade(85) // A
| Feature | Procedure | Function |
|---|---|---|
| Returns a value? | No | Yes - must include RETURN |
| How to call | As a standalone statement | As part of an expression or assignment |
| Typical use | Displaying output, modifying data | Calculations, data lookups, conversions |
| Example | displayMenu() | calculateAverage(80, 90, 70) |
// 'name' and 'age' are PARAMETERS (placeholders)
PROCEDURE displayInfo(name, age)
OUTPUT name & " is " & STR(age) & " years old"
ENDPROCEDURE
// "Alice" and 16 are ARGUMENTS (actual values)
displayInfo("Alice", 16)
FUNCTION add(a, b)
RETURN a + b // Returns the sum
ENDFUNCTION
FUNCTION isEven(number)
IF number MOD 2 = 0 THEN
RETURN TRUE // Returns Boolean TRUE
ELSE
RETURN FALSE // Returns Boolean FALSE
ENDIF
ENDFUNCTION
FUNCTION maxOf(a, b)
IF a > b THEN
RETURN a
ELSE
RETURN b
ENDIF
ENDFUNCTION
result ← add(3, 7) // result = 10
check ← isEven(4) // check = TRUE
bigger ← maxOf(15, 8) // bigger = 15
counter ← 0 // Global variable
PROCEDURE increment()
localCount ← 0 // Local variable
counter ← counter + 1 // Modifies global
localCount ← localCount + 1
OUTPUT "Local: ", localCount
ENDPROCEDURE
increment() // Output: Local: 1
increment() // Output: Local: 1 (localCount resets each call)
OUTPUT "Global: ", counter // Output: Global: 2
| Feature | Local Variable | Global Variable |
|---|---|---|
| Where declared | Inside a subroutine | At the top level of the program |
| Accessible from | Only within that subroutine | Anywhere in the program |
| Lifetime | Created and destroyed with each call | Exists for the entire program run |
| Risk of bugs | Low - isolated scope | High - can be changed accidentally |
| Best practice | Preferred - use local where possible | Minimise use to avoid side effects |
| Benefit | Explanation |
|---|---|
| Reusability | Write once, call many times. A calculateTax() function can be used in multiple places without rewriting the code. |
| Readability | Subroutine names describe what the code does. displayInvoice() is clearer than 50 lines of code. |
| Debugging | Test each subroutine independently. If a bug occurs, you know which subroutine to check. |
| Teamwork | Different programmers can work on different subroutines simultaneously. |
| Maintainability | Fix a bug in one place and it is fixed everywhere. Update one subroutine instead of finding every copy. |
| Abstraction | Call a subroutine without needing to know how it works internally. Just know what it does and what it returns. |
Q1: What is the difference between a procedure and a function?
Q2: Write a function called calculatePerimeter that takes the length and width of a rectangle and returns the perimeter.
Q3: Explain the difference between a parameter and an argument.
Q4: What is the advantage of using local variables instead of global variables?
Q5: Write a procedure called displayGrade that takes a score and outputs "Pass" if the score is 50 or above, or "Fail" otherwise.
FUNCTION calculatePerimeter(length, width)
perimeter ← 2 * (length + width)
RETURN perimeter
ENDFUNCTION
PROCEDURE displayGrade(score)
IF score >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDPROCEDURE
✗ Confusing parameters and arguments ✓ Parameters are the variables defined in the subroutine header; arguments are the actual values passed when the subroutine is called.
✗ Thinking procedures and functions are the same ✓ A function RETURNS a value; a procedure does NOT return a value. Use a function when you need a result back; use a procedure for actions like displaying output.
✗ Not understanding local vs global variable scope ✓ A local variable exists only inside the subroutine where it is declared. A global variable can be accessed anywhere. Local variables prevent accidental modification of data.
✗ Forgetting to use RETURN in a function ✓ A function must include a RETURN statement to send a value back to the calling code. Without RETURN, the function behaves like a procedure and the calling code receives no result.
Write a function called CalculateGrade that takes a test score as a parameter and returns a grade: 'A' for 80+, 'B' for 60-79, 'C' for 40-59, 'U' for below 40. Then write a procedure called DisplayResult that takes a name and grade and outputs a message. Show how both would be called. [6 marks]
FUNCTION CalculateGrade(score : INTEGER) RETURNS STRING IF score >= 80 THEN RETURN 'A' ELSE IF score >= 60 THEN RETURN 'B' ELSE IF score >= 40 THEN RETURN 'C' ELSE RETURN 'U' ENDIF ENDFUNCTION PROCEDURE DisplayResult(name : STRING, grade : STRING) OUTPUT name & ' achieved grade ' & grade ENDPROCEDURE grade ← CalculateGrade(75) DisplayResult('Alice', grade)
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including subroutines: functions and procedures for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including subroutines: functions and procedures to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including subroutines: functions and procedures, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.