Data types, control structures, functions and scope: WACE Computer Science Unit 3
“Use data types, operators, control structures (sequence, selection, iteration) and functions with parameters, return values and scope to design, trace and write algorithms in pseudocode and a programming language”
Programs combine data types and operators with three control structures: sequence, selection and iteration. Functions take parameters and return values, and local scope reduces side effects. Use trace tables to predict output and find errors when reading or writing pseudocode.
Jump to a section
What this dot point is asking
Programming is the largest content area in Unit 3. You need to use data types and operators correctly, build algorithms from sequence, selection and iteration, write functions with parameters and return values, understand scope, and trace code accurately. Exam questions often give pseudocode to trace or ask you to write a short algorithm.
The answer
Data types and operators
| Type | Example |
|---|---|
| Integer | 42 |
| Real (float) | 3.75 |
| Boolean | TRUE |
| Character or string | "A", "Perth" |
- Arithmetic: +, -, *, /, DIV (integer division), MOD (remainder), exponent.
- Relational: =, <>, <, >, <=, >=.
- Logical: AND, OR, NOT. Remember precedence: NOT, then AND, then OR (use brackets to be clear).
Control structures
- Sequence: steps in order.
- Selection: IF...THEN...ELSE, nested IFs, CASE statements.
- Iteration: FOR (count-controlled), WHILE (pre-test), REPEAT...UNTIL (post-test).
Functions and scope
- A function is a named, reusable block that takes parameters and returns a value. A procedure performs an action without returning a value.
- Local variables exist only inside their function; global variables are visible everywhere. Prefer local variables and parameters to reduce side effects.
- Modularity: breaking programs into functions makes them easier to read, test and reuse.
Trace tables
A trace table records the value of each variable after each step. It is the most reliable way to predict output and find logic errors.
Trace: n = 5; result = 1; WHILE n > 1: result = result * n; n = n - 1; END WHILE; OUTPUT result
| n | result |
|---|---|
| 5 | 1 |
| 4 | 5 |
| 3 | 20 |
| 2 | 60 |
| 1 | 120 |
Output: 120 (5 factorial). The loop stops when n = 1 because the condition n > 1 is false.
Off-by-one errors. Check whether loops should include the end value.
Confusing = (comparison) with assignment. Follow the pseudocode conventions in the exam.
Forgetting to return a value from a function.
Practice questions
Original practice questions graded from foundation to exam level, each with a full worked solution. Try them before revealing the solution.
foundation3 marksTrace the algorithm and state the output: total = 0; FOR i = 1 TO 4: IF i MOD 2 = 0 THEN total = total + i; END IF; NEXT i; OUTPUT totalShow worked solution →
| i | i MOD 2 = 0? | total |
|---|---|---|
| 1 | No | 0 |
| 2 | Yes | 2 |
| 3 | No | 2 |
| 4 | Yes | 6 |
Output: 6 (the sum of even numbers from 1 to 4).
Marking guide: 2 marks for a correct trace, 1 mark for the output.
core4 marksWrite pseudocode for a function isValidPin(pin) that returns TRUE only if pin is a string of exactly 4 characters and every character is a digit.Show worked solution →
FUNCTION isValidPin(pin)
IF LENGTH(pin) <> 4 THEN
RETURN FALSE
END IF
FOR i = 0 TO 3
IF pin[i] < "0" OR pin[i] > "9" THEN
RETURN FALSE
END IF
NEXT i
RETURN TRUE
END FUNCTION
Marking guide: 1 mark for the length check, 1 mark for the loop over characters, 1 mark for the digit test, 1 mark for correct return values.
exam5 marksExplain, with an example, why using a global variable for a running total can cause errors, and rewrite the logic using a function with a parameter and a return value.Show worked solution →
If several functions read and change a global variable such as total, one function can change it unexpectedly while another is still using it, causing hard-to-find logic errors. Global state also makes functions harder to test in isolation.
Example problem: addItem() and applyDiscount() both modify global total; calling applyDiscount twice by mistake silently changes the result everywhere.
Improved version:
FUNCTION addItem(currentTotal, price)
RETURN currentTotal + price
END FUNCTION
total = 0
total = addItem(total, 4.50)
total = addItem(total, 3.20)
The function works only with its parameter and returns a new value, so its effect is clear and testable, and total is changed in only one visible place.
Marking guide: 2 marks for explaining the risk of global scope, 1 mark for an example, 2 marks for a correct rewrite with parameter and return.
core4 marksGiven a = 7, b = 3 and flag = FALSE, evaluate each expression, showing your working: (i) a DIV b (ii) a MOD b (iii) NOT flag AND a > b (iv) a > 5 OR b = 3 AND flagShow worked solution →
(i) a DIV b = 7 DIV 3 = 2 (integer division discards the remainder).
(ii) a MOD b = 7 MOD 3 = 1 (7 = 2 x 3 + 1).
(iii) NOT is applied first: NOT FALSE = TRUE. Then a > b is 7 > 3 = TRUE. TRUE AND TRUE = TRUE.
(iv) AND has higher precedence than OR, so evaluate b = 3 AND flag first: TRUE AND FALSE = FALSE. Then a > 5 is 7 > 5 = TRUE. TRUE OR FALSE = TRUE. (Evaluating OR first would wrongly give (TRUE OR TRUE) AND FALSE = FALSE.)
Marking guide: 1 mark for each correct value with working (4 marks). In (iv), no mark if OR is evaluated before AND.
exam5 marksConsider the algorithm: x = 1; count = 0; REPEAT: x = x * 3; count = count + 1; UNTIL x > 50; OUTPUT x, count. (a) Complete a trace table and state the output. (3 marks) (b) The first line is changed to x = 100. State the new output, and explain how the output would differ if the loop were written as a pre-test loop, WHILE x <= 50, with the same body. (2 marks)Show worked solution →
(a)
| Iteration | x | count | x > 50? |
|---|---|---|---|
| start | 1 | 0 | |
| 1 | 3 | 1 | No |
| 2 | 9 | 2 | No |
| 3 | 27 | 3 | No |
| 4 | 81 | 4 | Yes, loop ends |
Output: 81, 4
(b) With x = 100, the REPEAT UNTIL loop is post-test, so the body runs once before the condition is checked: x = 300 and count = 1, then 300 > 50 ends the loop. Output: 300, 1.
With WHILE x <= 50, the condition is checked before the body. 100 <= 50 is false, so the body runs zero times and the output is 100, 0.
Marking guide: (a) 2 marks for a correct trace table, 1 mark for the output 81, 4. (b) 1 mark for 300, 1; 1 mark for 100, 0 with the pre-test explanation. Total 5 marks.
exam15 marksA school canteen app calculates the cost of a bulk order with a function calcPrice(basePrice, qty, isMember). The rules are: the cost is basePrice multiplied by qty; if qty is 10 or more, the cost is reduced by 10%; then, if the customer is a member and the cost is more than 50 dollars, a further 5 dollars is taken off. (a) State the most suitable data type for basePrice, qty, isMember and the value returned, and justify one of your choices. (3 marks) (b) Write the function calcPrice in pseudocode using selection. (5 marks) (c) Using your function, state the value returned by each call and show your working: calcPrice(4.00, 12, TRUE); calcPrice(6.00, 10, TRUE); calcPrice(6.00, 9, TRUE); calcPrice(7.50, 8, FALSE). (4 marks) (d) Inside calcPrice, a variable cost is used to hold the running cost. Explain the scope of cost, and why passing values as parameters is better than having calcPrice read global variables. (3 marks)Show worked solution →
(a) basePrice: real (float), because prices include cents such as 7.50. qty: integer, because you cannot order part of an item. isMember: Boolean, because it is only TRUE or FALSE. Returned value: real (float), because the cost can include cents after the discount.
(b)
FUNCTION calcPrice(basePrice, qty, isMember)
cost = basePrice * qty
IF qty >= 10 THEN
cost = cost * 0.9
END IF
IF isMember AND cost > 50 THEN
cost = cost - 5
END IF
RETURN cost
END FUNCTION
(c)
- calcPrice(4.00, 12, TRUE): 4.00 x 12 = 48.00; qty >= 10 so 48.00 x 0.9 = 43.20; member but 43.20 is not more than 50, so 43.20.
- calcPrice(6.00, 10, TRUE): 6.00 x 10 = 60.00; 60.00 x 0.9 = 54.00; member and 54.00 > 50, so 54.00 minus 5 = 49.00.
- calcPrice(6.00, 9, TRUE): 6.00 x 9 = 54.00; qty is less than 10, no discount; member and 54.00 > 50, so 49.00.
- calcPrice(7.50, 8, FALSE): 7.50 x 8 = 60.00; no bulk discount; not a member, so 60.00.
(d) cost is a local variable: it is created when calcPrice is called, can only be used inside the function, and no longer exists after the function returns. Code outside the function cannot change it by accident.
Passing values as parameters means the function depends only on its inputs and returns its result, so the same inputs always give the same output. It can be tested on its own with known values, reused for any order, and no other part of the program can silently change a global value it relies on. Reading globals creates hidden side effects and makes errors hard to trace.
Marking guide: (a) 2 marks for all four types correct (1 mark for two or three correct), 1 mark for a justification (3 marks). (b) 1 mark for calculating basePrice x qty, 1 mark for the qty >= 10 test, 1 mark for the 10% reduction, 1 mark for the combined member AND cost > 50 test with the 5 dollar reduction applied after the bulk discount, 1 mark for returning the cost (5 marks). (c) 1 mark per correct value with working (4 marks). (d) 1 mark for defining local scope, 1 mark for its lifetime or protection from outside code, 1 mark for the benefit of parameters over globals (3 marks). Total 15 marks.
exam17 marksA school weather station stores a week's maximum temperatures in an array temps, indexed from 0. A student wrote this function to count the days hotter than a limit: FUNCTION countAbove(temps, limit); count = 0; FOR i = 1 TO LENGTH(temps) - 1; IF temps[i] > limit THEN count = count + 1; END IF; NEXT i; RETURN count; END FUNCTION. The FOR loop includes its end value. (a) Complete a trace table for countAbove([31, 28, 35, 40, 29], 30), showing i, temps[i] and count, and state the value returned. (4 marks) (b) The correct answer is 3. Explain the cause of the error and write the corrected line. (3 marks) (c) Write a function averageOf(temps) in pseudocode that uses a WHILE loop to return the mean of the values in temps, and state what it returns for [31, 28, 35, 40, 29]. (5 marks) (d) The main program contains the call hot = countAbove(week, 30). Using this call, distinguish between a parameter and an argument, and explain why a variable named count in the main program would not be changed by the function. (3 marks) (e) State a suitable data type for limit and for the value returned by countAbove, with a reason for each. (2 marks)Show worked solution →
(a)
| i | temps[i] | temps[i] > 30? | count |
|---|---|---|---|
| start | 0 | ||
| 1 | 28 | No | 0 |
| 2 | 35 | Yes | 1 |
| 3 | 40 | Yes | 2 |
| 4 | 29 | No | 2 |
Returned value: 2
(b) The array is indexed from 0, but the loop starts at 1, so temps[0] (31, which is above 30) is never checked. This is an off-by-one error. Corrected line:
FOR i = 0 TO LENGTH(temps) - 1
(c)
FUNCTION averageOf(temps)
total = 0
i = 0
WHILE i < LENGTH(temps)
total = total + temps[i]
i = i + 1
END WHILE
RETURN total / LENGTH(temps)
END FUNCTION
For [31, 28, 35, 40, 29]: total = 163, and 163 / 5 = 32.6.
(d) A parameter is the variable named in the function definition that receives a value: temps and limit. An argument is the actual value passed in the call: week and 30. The count inside countAbove is a local variable, which exists only while the function runs. A count in the main program is a different variable in a different scope, so the function cannot change it; only the returned value is passed back, and it is stored in hot.
(e) limit: real (float), because a temperature limit could include a decimal such as 30.5. Returned value: integer, because it counts whole days.
Marking guide: (a) 3 marks for a correct trace table (1 mark for correct i values, 1 mark for correct temps[i] values, 1 mark for correct count values), 1 mark for returning 2 (4 marks). (b) 1 mark for identifying that index 0 is skipped, 1 mark for linking this to 0-based indexing (off-by-one), 1 mark for the corrected line (3 marks). (c) 1 mark for initialising total and i, 1 mark for a correct WHILE condition, 1 mark for accumulating and incrementing i, 1 mark for returning total divided by the length, 1 mark for 32.6 (5 marks). (d) 1 mark for parameter, 1 mark for argument, 1 mark for explaining local scope (3 marks). (e) 1 mark for each type with a reason (2 marks). Total 17 marks.