← IB Computer Science

Functions and Collections

As programs grow, you break them into reusable functions and store data in collections. This unit covers modularization, the parameters and return values of a function, local versus global scope, and collections: one-dimensional and two-dimensional lists, plus the difference between static and dynamic data structures. This is core B2.2 and B2.3 material for both SL and HL, so there is no HL-only content here.

Slide 1 / 4

Subprograms and modularization

Modularization: split code into focused functionsOne bigprogramcalculateBonus( )one task: work out a bonusrectangleVolume( )one task: volume of a boxprintReport( )one task: display resultsEasier to• read• test• reuse• updateName each function for its task, e.g. calculateBonus or rectangleVolume.
  • Modularization is splitting code into separate functions or modules, each performing one task.
  • It makes code easier to read, because you can see where a task is used, and easier to test in isolation.
  • It makes code easier to reuse and update, since the same module can be imported into many programs.
  • Give a function a name that describes its task, for example calculateBonus or rectangleVolume.

Check you've got it

0/4

1. In the function definition public static int rectangleVolume(int w, int h, int d), what are w, h and d?

2. A one-dimensional list stores the values [23, 17, 4839, 606]. What is stored at index 1?

3. Which of the following is a dynamic data structure?

4. A two-dimensional list temperature holds row 2 as [24, 28, 29, 31, 32, 31, 33]. Access is [row][column] with indexing from 0. What does temperature[2][3] return?

Free revision content. Multiple-choice answers are marked instantly. Written answers are marked by AI on /free-mark.