Knowledge organisersAdditional Programming Techniques
passing and returning
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.
# 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]