Define and use procedures and functions with or without parameters

Cambridge IGCSE Computer Science – Full Syllabus Notes (0478)

Learning Objectives

Define and use procedures and functions (with or without parameters) and integrate them with variables, I/O, control structures, totalling, counting, searching, sorting, and the broader concepts required by the Cambridge IGCSE syllabus.


1. Data Representation

1.1 Number Systems

SystemBaseDigits UsedTypical Use in CS
Binary20, 1Machine language, bit‑wise operations
Octal80‑7Compact representation of binary groups
Hexadecimal160‑9, A‑FMemory addresses, colour codes
Decimal100‑9Human‑readable numbers

Conversions (pseudocode)

FUNCTION BinToDec(binString)
BEGIN
    DECLARE result, i, digit : INTEGER
    result ← 0
    FOR i ← 1 TO LENGTH(binString) DO
        digit ← CHAR_TO_INT(binString[i])          // '0'→0, '1'→1
        result ← result * 2 + digit
    END FOR
    RETURN result
END FUNCTION

1.2 Two’s‑Complement & Arithmetic

  • Positive numbers: same as binary.
  • Negative numbers: invert bits, add 1.
  • Overflow occurs when the sign bit changes incorrectly (e.g., adding two positive numbers yields a negative result).

1.3 Text, Sound & Images

  • Text: ASCII (7‑bit) or Unicode (UTF‑8/16). One character = 1 byte (ASCII) or up to 4 bytes (Unicode).
  • Sound: Sample rate (samples / second) × bit depth × channels = data rate. Example: 44 kHz, 16‑bit, stereo → 44 000 × 16 × 2 = 1 408 000 bits ≈ 176 KB/s.
  • Images: Pixels × colour‑depth. Example: 800 × 600 pixels, 24‑bit colour → 800 × 600 × 24 = 11 520 000 bits ≈ 1.44 MB.

1.4 Data Storage & Compression

  • Units: 1 byte = 8 bits; 1 KB = 2¹⁰ bytes; 1 MB = 2²⁰ bytes; 1 GB = 2³⁰ bytes.
  • Lossless compression (e.g., Run‑Length Encoding, Huffman coding) – original data can be perfectly reconstructed.
  • Lossy compression (e.g., JPEG for images, MP3 for audio) – some data is discarded to achieve higher compression ratios.

2. Data Transmission

  • Packet structure: Header (address, control bits) + Payload (data) + Trailer (error‑check).
  • Protocols: TCP/IP for reliable transmission; UDP for faster, connection‑less transfer.
  • USB: Serial bus, hot‑plug, supports multiple devices via hubs.
  • Error detection: Parity bit, checksum, CRC. Example of a simple parity check:
    IF (COUNT_OF_1s(data) MOD 2) = parityBit THEN
        // no error
    ELSE
        // error detected
    END IF
  • Encryption: Symmetric (same key for encrypt/decrypt) vs. Asymmetric (public/private key pair). Basic idea – transform plaintext using an algorithm and a key.

3. Hardware

  • CPU: Executes the Fetch‑Decode‑Execute (FDE) cycle.
    1. Fetch: Retrieve instruction from memory (address in Program Counter).
    2. Decode: Interpret opcode, identify operands.
    3. Execute: Perform operation using ALU, registers, or memory.
  • Registers: Small, fast storage inside CPU (e.g., Accumulator, Program Counter, Instruction Register).
  • Cache: Multi‑level (L1, L2) memory that stores recently accessed data to reduce latency.
  • Instruction set: Set of binary codes the CPU can execute (e.g., ADD, SUB, LOAD, STORE).
  • Embedded systems: Dedicated computers inside devices (e.g., microwaves, cars). Typically have limited I/O and run a single program.

4. Software

  • System software: Operating System (OS), device drivers, utility programs.
  • Application software: Word processors, browsers, games – built on top of the OS.
  • OS functions: Resource management, file management, multitasking, security.
  • Interrupts:
    • Hardware interrupt – external event (e.g., key press) signals the CPU.
    • Software interrupt – program‑generated request (e.g., system call).
  • Language translators: Assembler, compiler, interpreter, decompiler.
  • Integrated Development Environment (IDE): Provides editor, compiler/interpreter, debugger, and often a visual designer.

5. Internet & Its Uses

  • Internet vs. World Wide Web: Internet = global network of networks; WWW = collection of hyper‑text documents accessed via HTTP.
  • URL structure: protocol://domain[:port]/path?query#fragment
  • HTTP / HTTPS: Request‑response protocol; HTTPS adds TLS encryption.
  • Web browsers: Render HTML, CSS, JavaScript.
  • Cookies: Small data stored on the client to maintain state (e.g., session ID).
  • Digital currency: Uses cryptographic hashing and public‑key cryptography (e.g., Bitcoin, blockchain basics).
  • Cyber‑security checklist:
    • Use strong, unique passwords.
    • Keep software up‑to‑date.
    • Beware of phishing and social engineering.
    • Use firewalls and anti‑malware tools.
    • Encrypt sensitive data.

6. Automated & Emerging Technologies

  • Sensors: Convert physical quantities to electrical signals (e.g., temperature, light).
  • Actuators: Convert electrical signals into motion or action (e.g., motors, solenoids).
  • Robotics: Combination of sensors, actuators, and control algorithms. Simple example – line‑following robot using two light sensors.
  • Artificial Intelligence:
    • Expert systems – rule‑based reasoning (IF‑THEN rules).
    • Machine learning – algorithms that improve from data (e.g., decision trees, neural networks).

7. Algorithm Design & Problem Solving

  • Decomposition: Break a problem into smaller, manageable sub‑problems (often realised as procedures/functions).
  • Pseudocode conventions (Cambridge):
    • All keywords in UPPERCASE.
    • Identifiers in Pascal‑case (e.g., TotalScore).
    • Indentation indicates block structure.
  • Validation & Verification:
    • Validation – “Did we build the right thing?” (check requirements).
    • Verification – “Did we build it correctly?” (check logic, test data).
  • Trace tables – record the value of each variable after every step to prove correctness.
  • Testing:
    • Boundary‑value testing, equivalence partitioning, and “happy‑path” cases.

8. Variables, Constants & Data Types

  • Variable declaration: DECLARE identifier : type
  • Constant declaration: CONSTANT identifier ← value
  • Data types required by the syllabus:
    • INTEGER – whole numbers (e.g., -32768 to 32767)
    • REAL – decimal numbers (floating‑point)
    • CHAR – single character
    • STRING – sequence of characters
    • BOOLEAN – TRUE or FALSE
  • Identifier rules (Cambridge):
    • Begin with a letter.
    • Contain only letters and digits (no underscores).
    • Case‑insensitive.
    • Use Pascal‑case for readability (e.g., TotalScore).

9. Basic Input / Output

CommandPurposeExample
INPUT identifier Read a value from the user and store it in a variable. INPUT Age
OUTPUT expression(s) Display text or the value of an expression. OUTPUT "Your age is ", Age

10. Control Structures

StructureSyntax (pseudocode)Typical Use
Sequence Statements are executed one after another. Simple processing steps.
Selection – IF IF condition THEN
  statements
ELSE
  statements
END IF
Choose between two alternatives.
Selection – CASE CASE expression OF
  value1: …
  value2: …
  OTHER: …
END CASE
Multiple‑choice selection.
Iteration – FOR FOR counter ← start TO end STEP step DO
  statements
END FOR
Known number of repetitions.
Iteration – WHILE WHILE condition DO
  statements
END WHILE
Repeat while a condition remains true.
Iteration – REPEAT … UNTIL REPEAT
  statements
UNTIL condition
Execute at least once, then test.

11. Procedures and Functions (with or without Parameters)

11.1 Definitions

  • Procedure: A named block that performs an action (e.g., I/O, modifying data) but does **not** return a value.
  • Function: A named block that computes a value and returns exactly one result of a defined type.

11.2 Procedure vs. Function – Comparison

AspectProcedureFunction
PurposePerforms an action (e.g., display, update data)Computes a value
Return valueNone (void)Exactly one value of a defined type
Typical useDisplaying results, changing global structuresMathematical calculations, data transformation
Call syntaxCALL ProcedureName(arg1, arg2)result ← FunctionName(arg1, arg2)
Parameter passingBy value or by reference (language‑dependent)Usually by value; some languages allow reference parameters

11.3 Parameter‑Passing Techniques

  • Pass‑by‑value: A copy of the argument is supplied; changes inside the routine do not affect the original variable.
  • Pass‑by‑reference: The address of the argument is supplied; modifications affect the caller’s variable.

11.4 Scope and Lifetime

Parameters and variables declared inside a procedure or function are local. Their scope begins at the start of the routine and ends when the routine terminates. Global variables (declared outside any routine) remain accessible throughout the program.

11.5 Common Pitfalls

  • Mismatch in number, order, or type of arguments versus parameters.
  • Attempting to return a value from a procedure – illegal in most Cambridge‑compatible languages.
  • Confusing pass‑by‑value with pass‑by‑reference, leading to unintended side‑effects.
  • Using a function for a task that only requires output; this wastes the return value.

12. Using Parameters – Examples

12.1 Procedure without parameters

PROCEDURE ShowWelcomeMessage()
BEGIN
    OUTPUT "Welcome to the IGCSE Computer Science exam!"
END PROCEDURE

12.2 Procedure with parameters (pass‑by‑value)

PROCEDURE PrintSum(a, b)
BEGIN
    DECLARE sum : INTEGER
    sum ← a + b
    OUTPUT "The sum is ", sum
END PROCEDURE

12.3 Procedure with parameters (pass‑by‑reference)

PROCEDURE IncrementByOne(REF number)
BEGIN
    number ← number + 1
END PROCEDURE

12.4 Function without parameters

FUNCTION GetCurrentYear()
BEGIN
    RETURN 2025          // static example; in real code you would query the system clock
END FUNCTION

12.5 Function with parameters (pass‑by‑value)

FUNCTION Square(x)
BEGIN
    RETURN x * x
END FUNCTION

12.6 Using a function inside an expression

area ← PI * Square(radius)

13. Totalling & Counting (Pattern)

PROCEDURE TotalAndCount(numbers[])
BEGIN
    DECLARE total, count, i : INTEGER
    total ← 0
    count ← 0
    FOR i ← 1 TO LENGTH(numbers) DO
        total ← total + numbers[i]
        count ← count + 1
    END FOR
    OUTPUT "Total = ", total, ", Count = ", count
END PROCEDURE

14. Searching & Sorting (Mandatory Algorithms)

14.1 Linear Search (Procedure)

PROCEDURE LinearSearch(data[], key)
BEGIN
    DECLARE i, position : INTEGER
    position ← 0                     // 0 = not found
    FOR i ← 1 TO LENGTH(data) DO
        IF data[i] = key THEN
            position ← i
            EXIT FOR                // stop as soon as it is found
        END IF
    END FOR
    IF position = 0 THEN
        OUTPUT "Key not present"
    ELSE
        OUTPUT "Key found at position ", position
    END IF
END PROCEDURE

14.2 Bubble Sort (Function returning a sorted array)

FUNCTION BubbleSort(list[])
BEGIN
    DECLARE n, i, j, temp : INTEGER
    n ← LENGTH(list)
    FOR i ← 1 TO n-1 DO
        FOR j ← 1 TO n-i DO
            IF list[j] > list[j+1] THEN
                temp ← list[j]
                list[j] ← list[j+1]
                list[j+1] ← temp
            END IF
        END FOR
    END FOR
    RETURN list
END FUNCTION

15. Databases – Single‑Table Design & Basic SQL

  • Table components: Table name, fields (columns), records (rows).
  • Field types: INTEGER, REAL, CHAR, VARCHAR, DATE, BOOLEAN.
  • Primary key: A field (or combination) that uniquely identifies each record.

15.1 Example Table – Students

StudentID (PK)NameAgeScore
1001Alice1478
1002Bob1585
1003Carol1492

15.2 Basic SQL Commands (Cambridge subset)

  • SELECT: Retrieve data.
    SELECT Name, Score FROM Students WHERE Age = 14 ORDER BY Score DESC;
  • SUM, COUNT (aggregation):
    SELECT COUNT(*) AS NumStudents, SUM(Score) AS TotalScore FROM Students;
  • INSERT:
    INSERT INTO Students (StudentID, Name, Age, Score) VALUES (1004, 'Dave', 15, 88);
  • UPDATE:
    UPDATE Students SET Score = Score + 5 WHERE StudentID = 1002;
  • DELETE:
    DELETE FROM Students WHERE StudentID = 1003;

16. Boolean Logic

GateSymbolTruth Table
AND AND gate
A B | A∧B
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1
OR OR gate
A B | A∨B
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
NOT NOT gate
A | ¬A
0 | 1
1 | 0
NAND NAND gate
A B | ¬(A∧B)
0 0 | 1
0 1 | 1
1 0 | 1
1 1 | 0
NOR NOR gate
A B | ¬(A∨B)
0 0 | 1
0 1 | 0
1 0 | 0
1 1 | 0
XOR XOR gate
A B | A⊕B
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0

Logical Operators in Pseudocode

  • AND – both conditions true.
  • OR – at least one condition true.
  • NOT – negates a condition.
  • Combined example:
    IF (age >= 18) AND (hasID = TRUE) THEN
        OUTPUT "Access granted"
    ELSE
        OUTPUT "Access denied"
    END IF

17. Integrated Example – Conversion, Display & Database Update

FUNCTION ConvertCtoF(celsius)
BEGIN
    RETURN (celsius * 9/5) + 32
END FUNCTION

PROCEDURE DisplayAndStore(tempC)
BEGIN
    DECLARE tempF : REAL
    tempF ← ConvertCtoF(tempC)
    OUTPUT tempC, "°C = ", tempF, "°F"

    // Store the conversion in a simple table (pseudo‑SQL)
    CALL ExecuteSQL("INSERT INTO Conversions (Celsius, Fahrenheit) VALUES (" &
                    tempC & ", " & tempF & ");")
END PROCEDURE

CALL DisplayAndStore(25)

18. Summary Checklist (AO1‑AO3)

  • Identify when a task should be a procedure (no return) or a function (returns a value).
  • Declare variables, constants and parameters using correct Cambridge syntax.
  • Write clear INPUT and OUTPUT statements.
  • Choose appropriate control structures (IF, CASE, FOR, WHILE, REPEAT) and nest them correctly.
  • Implement totalling, counting, linear search and bubble sort using procedures/functions.
  • Match arguments to parameters in number, order and type; be aware of pass‑by‑value vs. pass‑by‑reference.
  • Remember that parameters are local; their scope ends when the routine finishes.
  • Use trace tables and test data to verify correctness (AO2) and to debug logical errors (AO3).
  • Apply knowledge of data representation, transmission, hardware, software, internet, emerging tech, databases and Boolean logic where relevant to the problem.

Create an account or Login to take a Quiz

41 views
0 improvement suggestions

Log in to suggest improvements to this note.