Functions and Procedures: arrays
2.2.3 Additional Programming Techniques
Practise exam questionsAdditional Programming Techniques — Note 11 of 12
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.
arr[0] is the first element and arr[len(arr) - 1] is the last. Using an index outside this range causes an error.for i in range(len(arr)): so the same function works for an array of ANY size.return as soon as the item is found. Only return 'not found' (e.g. -1) AFTER the loop has checked every element.arr[0]), not 0, so the function still works if every value is negative.arr[i] = arr[i] * 2) rather than adding new items.arr[i] inside the sub-program also changes the original array in the main program.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.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 indef 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.4def 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)# 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)) # 3def 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])) # 25def 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)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