Form 4 · Chapter 1

Data Structures and Modularity

Arrays store many values under one name, while functions and modular design break a program into small, reusable parts.

Arrays

An array is a data structure that stores many values of the same type under one name. Each value is called an element and is accessed using an index (a position number). In most languages the index starts at 0, so the first element is at index 0.

Example

marks = [80, 65, 90, 72]
# marks[0] is 80
# marks[2] is 90

One array stores four marks without needing four separate variables.

Functions and Subroutines

A function (or subroutine) is a named block of code that performs a specific task and can be called repeatedly. A function can accept parameters (input values passed in) and can return a result value.

Example

FUNCTION area(length, width)
  RETURN length * width
END FUNCTION

# call: area(5, 3) returns 15

length and width are parameters.

Modular Design

Modular design breaks a large program into several small modules or functions, each doing one clear task. Its advantages include:

  • Reusability — one function can be called many times without rewriting the code.
  • Easier testing, because each module can be tested separately.
  • Easier maintenance and understanding.

Key idea

A parameter is a value passed into a function; a return value is the result the function gives back to the part that called it.

Remember

Use an array when you need to store many similar values, and use a function when the same task must be done more than once.

Stuck on this topic? A verified JomKelas tutor can walk you through it.

Find a verified tutor