Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Additional Programming Techniques

Functions and Procedures: arrays

All topicsPractise exam questions
Knowledge organiser

Additional Programming Techniques

2.2.3k

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.

Key points

  • 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.

Code examples

Passing and returning arrays
python
# Function that takes an array and returns a new one
def double_values(numbers):
    result = []
    for num in numbers:
        result.append(num * 2)
    return result

original = [1, 2, 3, 4, 5]
doubled = double_values(original)
print(doubled)  # Output: [2, 4, 6, 8, 10]