Programming Practice: Writing and Refining Programs
Programming practice covers building complete programs by combining procedures and functions with parameters, 2D arrays traversed with nested loops, string-handling operations and file handling, and understanding how a compiler or interpreter translates source code into a form the computer can run. It also covers refining and improving existing code, such as renaming identifiers, removing duplication with a subroutine and adding comments, without changing what the program actually does. It is assessed throughout Paper 2 of GCSE Computer Science, usually through short pseudocode-writing and code-reading questions.
Before you start
Make sure you're comfortable with these topics first:
Method
- Learn the difference between a procedure, which carries out a task but does not return a value, and a function, which returns exactly one value to the code that called it, and how to pass parameters into each.
- Practice declaring and using a 2D array with nested loops: the outer loop moves through each row index, and the inner loop moves through each column index within that row.
- Learn core string-handling operations: LENGTH returns the number of characters in a string, SUBSTRING extracts part of a string, the '&' operator joins (concatenates) two strings, and ASC/CHR convert between a character and its ASCII code.
- Learn the standard sequence for handling a text file: OPEN the file, READ or WRITE the data needed, then CLOSE the file, and why forgetting to close a file risks losing unsaved data or leaving the file locked.
- Learn the difference between a compiler, which translates the whole source code into machine code before the program runs, and an interpreter, which translates and executes the code one line at a time.
- Practice refining given code: renaming poor identifiers to meaningful names, replacing repeated code with a subroutine call, adding comments, and simplifying overly nested selection, without changing what the program does.
Worked example
A 2D array called scores stores marks for 3 students (rows 0-2) across 2 tests (columns 0-1): scores = [[45,60],[72,68],[80,90]]. Write a function studentTotal(row) that returns the total of both test scores for the student at that row, then state what studentTotal(1) returns.
- Identify that the function needs one parameter, row, and must RETURN a single value: the sum of scores[row][0] and scores[row][1].
- Write the function: FUNCTION studentTotal(row) RETURN scores[row][0] + scores[row][1] ENDFUNCTION
- Trace the call studentTotal(1) by substituting row = 1.
- Look up the values at that row: scores[1][0] = 72 and scores[1][1] = 68.
- Add the two values together: 72 + 68 = 140.
- Final answer: studentTotal(1) returns 140.
Practice questions
Type your answer and press Check to be marked straight away, or reveal the answer and mark yourself.
Q1State the key difference between a procedure and a function.Show answer
Answer: A function always returns a single value to the code that called it; a procedure carries out a task but does not return a value.
Q2Write the pseudocode header and body for a function called isAdult that takes one integer parameter called age and returns True if age is 18 or over.Show answer
Answer: FUNCTION isAdult(age) RETURN age >= 18 ENDFUNCTION
Q3State the pseudocode operator commonly used to join (concatenate) two strings, then show how it joins 'Rev' and 'ision' to produce 'Revision'.Show answer
Answer: The '&' operator; 'Rev' & 'ision' = 'Revision'
Q4State the value returned by LENGTH(word) if word stores the string 'Computer'.Show answer
Answer: 8 (C, o, m, p, u, t, e, r is 8 characters)
Q5State the correct order of the three operations used to read data from a text file.Show answer
Answer: OPEN the file, READ the data, then CLOSE the file
Q6A programmer wants any syntax error in their program to be reported before the program starts running at all. State whether a compiler or an interpreter behaves this way, and explain why.Show answer
Answer: A compiler, because it translates the entire source code into machine code before execution begins, so it checks the whole program's syntax first; an interpreter translates and runs one line at a time, so earlier lines would already have executed before it reached a later syntax error.
Q7Give one specific way this code could be refined without changing what it does: a = 5, b = 3, c = a + b, PRINT cShow answer
Answer: Rename the single-letter variables to meaningful identifiers, e.g. a to firstNumber, b to secondNumber, c to total.
Q8A function is defined as: FUNCTION addTax(price) RETURN price x 1.2 ENDFUNCTION. Another line of code runs: total <- addTax(20) + addTax(30). Trace both function calls and state the final value of total.Show answer
Answer: addTax(20) returns 20 x 1.2 = 24; addTax(30) returns 30 x 1.2 = 36; total = 24 + 36 = 60
Exam-style questions
Written in the style of a GCSE Computer Science exam paper, with a full mark scheme.
A program includes a subroutine called validateAge that checks whether an integer parameter is between 0 and 120 inclusive, and returns a Boolean value. (a) State whether validateAge should be written as a procedure or a function, giving one reason. (b) Write the pseudocode header and body for validateAge.
Show mark scheme
Tick each line you got. Your score builds from the marks on the scheme.
Nothing ticked yet - 4 available
A 2D array called seating stores the number of empty seats in a small theatre, with 3 rows (0-2) and 4 columns (0-3): seating = [[2,0,1,3],[0,0,2,0],[1,4,0,2]]. Write an algorithm, using pseudocode, that uses a nested loop to add up and output the total number of empty seats in the whole theatre.
Show mark scheme
Tick each line you got. Your score builds from the marks on the scheme.
Nothing ticked yet - 5 available
A student has written working pseudocode for a program that totals the cost of items in a shopping basket, but it uses single-letter variable names, repeats the same three lines of calculation code four times instead of using a subroutine, and has no comments. Discuss how refining this code, without changing what it does, would improve the program, and evaluate whether these changes are worth making for a short program used only by the student themselves.
Show mark scheme
Tick each line you got. Your score builds from the marks on the scheme.
Nothing ticked yet - 6 available
See real GCSE Computer Science past-paper questions, with official mark schemes →
Free printable worksheet
Want more practice on paper? Download the programming practice: writing and refining programs worksheet pack - 23 pages of exam-style questions with a full mark scheme. One email opens every download in this browser for 14 days - no account, no card. Print it for personal and classroom use.
Next topics
Not quite what you needed?
Tell us what is missing on programming practice: writing and refining programs, or which topic to write up next. Every request is read, and we reply to every one.
Build a full practice pack.
This topic is one of hundreds in the library - pick the ones a student needs and generate a printable PDF in minutes.