ProceduralOCR J277
DashboardCoursePracticeReviewProgressGlossary
Saved on this deviceContinue learning

Procedural · OCR J277 programming practice

Progress stays in this browser unless you export it.

Course/Procedures & functions/Passing and returning arrays
J277 · 2.2.3

Unit 9 · Lesson 5

Passing and returning arrays

Lesson progress0 / 7

Build the idea

Start with the concept and its key vocabulary.

Key idea

Pass an array into a sub-program, iterate over its indices and return a processed array or value.

An array can be supplied as a parameter just like one number or string. The sub-program can inspect each element, calculate from it or build a new array to return. Passing the collection keeps the sub-program reusable and avoids a global array. Indices begin at 0, so an ERL loop normally runs from 0 to array.length - 1; Python uses range(len(array)).

Array parameter

A parameter that receives an array or Python list from the caller.

Index

A zero-based position used to access an array element.

Return array

Send a complete processed array back from a function.

Mutable

Able to be changed after creation; Python lists are mutable.

Keep these in mind

  • Arrays can be passed to procedures and functions.
  • A function may return an array as its result.
  • Use the loop counter as the array index.
  • For a length of 5, valid indices are 0 to 4.
  • Python lists passed to functions can also be modified in place because they are mutable.

Learning objective

Pass an array into a sub-program, iterate over its indices and return a processed array or value.

Exam tip

For an ERL array loop, write for index = 0 to values.length - 1 and access values[index] using the same counter name.

Common mistake

Starting at index 1 misses the first element. Also do not write values[item] in Python when item is already the element value rather than an index.

PreviousNext