Ad Banner Placeholder

Cambridge IGCSE Computer Science · 0478

Topic 8: Programming — Part 3

Strings, Subroutines & Arrays

String functions

Cambridge pseudocode provides built-in functions for working with strings. String positions are indexed from 1 (the first character is at position 1, not 0):

Function Purpose Example
LENGTH(S) Returns the number of characters in string S LENGTH("IGCSE") = 5
LCASE(S) Converts string S to lowercase LCASE("Hello") = "hello"
UCASE(S) Converts string S to uppercase UCASE("hi") = "HI"
SUBSTRING(S, start, length) Extracts length characters from string S starting at position start SUBSTRING("Computer", 1, 3) = "Com"
Word ← "Computer"
OUTPUT LENGTH(Word)              // 8
OUTPUT SUBSTRING(Word, 1, 3)     // "Com"
OUTPUT UCASE(SUBSTRING(Word, 1, 3))  // "COM"

Procedures and functions

Subroutines are named blocks of code that can be called from elsewhere in the program. They support modularisation — breaking a program into smaller, manageable parts.

Type Returns a value? Example call
Procedure No — performs a task only CALL DisplayMenu()
Function Yes — returns a value to the caller Area ← CalculateArea(Length, Width)

Parameters pass data into a subroutine. A subroutine definition lists the parameters it accepts; the call supplies matching values:

PROCEDURE Greet(Name : STRING)
    OUTPUT "Hello, ", Name
ENDPROCEDURE

CALL Greet("Sam")

Local variables are declared inside a subroutine and can only be used within that subroutine. Global variables are declared at the program level and can be accessed from anywhere in the program.

One-dimensional and two-dimensional arrays

An array stores multiple values of the same data type under one identifier. Array indices start at 1 in Cambridge pseudocode.

1D array

A one-dimensional array is like a single list of values:

DECLARE Scores[1:5] : INTEGER
Scores[1] ← 88
Scores[2] ← 72
Scores[3] ← 95

2D array

A two-dimensional array is like a table with rows and columns. For example, a 3×3 grid:

DECLARE Grid[1:3, 1:3] : INTEGER
Grid[1, 1] ← 1
Grid[2, 3] ← 9
3 by 3 two-dimensional array grid showing rows and columns with indexed cell positions
Diagram 1: A 3×3 two-dimensional array — each cell is accessed with a row index and a column index, e.g. Grid[2, 3].

Initialising a 2D array with nested loops

FOR Row ← 1 TO 3
    FOR Col ← 1 TO 3
        Grid[Row, Col] ← 0
    NEXT Col
NEXT Row

0/15

Ad Banner Placeholder