Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Paper 2/Additional Programming Techniques

arrays

Functions and Procedures: arrays

2.2.3 Additional Programming Techniques

Practise exam questions

Additional Programming Techniques — Note 11 of 12

passing and returning

What you need to know

Arrays can be passed as parameters to functions and procedures, allowing sub-programs to work with collections of data. Functions can also return arrays as their result. This is useful for tasks like sorting a list in a function and returning the sorted version, or processing all elements of an array in a procedure.

What to know

  • Arrays can be passed as parameters to functions and procedures.
  • The sub-program can then read, modify, or process the array's elements.
  • Functions can return an array as their result.
  • Passing arrays avoids the need for global variables to share collections of data.
  • In Python, lists (arrays) passed to functions can be modified in place because they are passed by reference.
  • This enables modular code: e.g. a sort function takes an array and returns it sorted.
  • INDEXING: arr[0] is the first element and arr[len(arr) - 1] is the last. Using an index outside this range causes an error.
  • LOOPING: inside a sub-program, loop with for i in range(len(arr)): so the same function works for an array of ANY size.
  • Each algorithm follows the same pattern: set up a variable BEFORE the loop (total, count, highest or found), update it INSIDE the loop, and use or return it AFTER the loop.
  • A function that searches should return as soon as the item is found. Only return 'not found' (e.g. -1) AFTER the loop has checked every element.
  • When finding the highest or lowest value, start with the FIRST element (arr[0]), not 0, so the function still works if every value is negative.
  • Arrays are a fixed size, so change an existing element by its index (arr[i] = arr[i] * 2) rather than adding new items.
  • Because the array is passed by reference, changing arr[i] inside the sub-program also changes the original array in the main program.

Exam technique

  • Mistake:Using append() to build or change an array. append adds a new item and changes the size, which an array cannot do. Exam answers should update elements by index.

Code examples

Indexing and looping through an array passed to a procedure
python
scores = [12, 7, 5, 10, 3]

def show_scores(arr):
    for i in range(len(arr)):          # i = 0, 1, 2, 3, 4
        print("Index", i, "holds", arr[i])

print(scores[0])                 # 12  first element
print(scores[len(scores) - 1])   # 3   last element
show_scores(scores)              # pass the whole array in
Total and average of an array (functions)
python
def total_of(arr):
    total = 0                    # set up BEFORE the loop
    for i in range(len(arr)):
        total += arr[i]          # add each element
    return total                 # return AFTER the loop

def average_of(arr):
    return total_of(arr) / len(arr)

scores = [12, 7, 5, 10, 3]
print("Total:", total_of(scores))      # Total: 37
print("Average:", average_of(scores))  # Average: 7.4
Linear search function: check if a value is in the array
python
def linear_search(arr, target):
    for i in range(len(arr)):    # check each element IN ORDER
        if arr[i] == target:
            return i             # found: return its index straight away
    return -1                    # only reached if nothing matched

scores = [12, 7, 5, 10, 3]
print(linear_search(scores, 10))   # 3
print(linear_search(scores, 99))   # -1 (not found)
Counting items in an array (functions)
python
# How many times does a value appear?
def count_value(arr, target):
    count = 0
    for i in range(len(arr)):
        if arr[i] == target:
            count += 1
    return count

# How many values are over a limit?
def count_over(arr, limit):
    count = 0
    for i in range(len(arr)):
        if arr[i] > limit:
            count += 1
    return count

scores = [12, 7, 12, 10, 3]
print(count_value(scores, 12))   # 2
print(count_over(scores, 8))     # 3
Finding the highest value in an array
python
def find_highest(arr):
    highest = arr[0]                 # start with the FIRST element
    for i in range(1, len(arr)):     # compare with the rest
        if arr[i] > highest:
            highest = arr[i]
    return highest

print(find_highest([12, 7, 25, 10, 3]))   # 25
Changing every element and returning the array
python
def double_values(arr):
    for i in range(len(arr)):
        arr[i] = arr[i] * 2          # update the element at index i
    return arr

original = [1, 2, 3, 4, 5]
doubled = double_values(original)
print(doubled)    # [2, 4, 6, 8, 10]
print(original)   # [2, 4, 6, 8, 10] (the original changed too: passed by reference)
OCR Exam Reference Language: total of an array
text
function totalOf(arr)
    total = 0
    for i = 0 to arr.length - 1
        total = total + arr[i]
    next i
    return total
endfunction

array scores[5]
scores = [12, 7, 5, 10, 3]
print(totalOf(scores))

Ready to test yourself?

Practise 2.2.3 questions
← Previous noteglobal variables2.2.3 Additional Programming TechniquesNext note →Random number generation2.2.3 Additional Programming Techniques

Back to Additional Programming Techniques

Revise another OCR GCSE topic: Programming Fundamentals

Additional Programming Techniques

Note 11 of 12

  1. 1Concatenation
  2. 2slicing
  3. 3Open, Read, Write, Close
  4. 4Records to store data
  5. 5SQL to search for data
  6. 6one-dimensional (1D)
  7. 7two-dimensional arrays(2D)
  8. 8Sub programs (functions and procedures) to produce structured code
  9. 9local variables
  10. 10global variables
  11. 11arrays
  12. 12Random number generation
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).