Procedures, functions and maintainable programs Cambridge IGCSE Computer Science revision

Not started

Learn it

In plain words

As programs grow, the same few lines turn up again and again. Sooner than copy them out, you give them a name and call them by that name whenever they are needed. A named block of code like this is a procedure or a function.

The difference between the two is small but important: a function hands a value back.

6 things to know

  1. A procedure is a named block of code that carries out a task. It is run with CALL: for example, CALL ShowMenu.
  2. A function is a named block of code that carries out a task and returns a value. It is used inside an expression: Area ← RectangleArea(4, 5).
  3. A parameter is a value passed into a procedure or function when it is called, so that the same code can work on different data.
  4. A local variable is declared inside a procedure or function and can only be used there. A global variable is declared in the main program and can be used anywhere.
  5. A program is maintainable when someone else can read it and change it. That means meaningful identifiers for variables, constants, arrays, procedures and functions.
  6. It also means comments that explain what the code does, and procedures and functions that keep each task in one place.

Worked example

A function is defined as FUNCTION Double(Number : INTEGER) RETURNS INTEGER, and its body is RETURN Number * 2. What does OUTPUT Double(7) + 1 display?

  1. Double is called with the parameter 7.
  2. Inside the function, Number is 7, so it returns 7 × 2 = 14.
  3. The expression becomes 14 + 1, so 15 is displayed.

Tips and tricks

  • A function returns a value and a procedure does not. If the question says "returns", it is a function.
  • Use local variables where you can. A global variable can be changed from anywhere, which makes errors hard to track down.
6 questions, about 2 minutes.

It lands in your notebook with its questions as flashcards.

Procedures, functions and maintainable programs: 6 questions and answers

These are the quiz’s questions. Do the quiz first, then come back here for the ones that got you.

  1. What is the difference between a function and a procedure?
    • A function has no name.
    • A function returns a value. (the answer)
    • A procedure cannot take parameters.
    • A procedure runs faster.

    Both are named blocks of code.

  2. What is a parameter?
    • a type of loop
    • a value passed into a procedure or function (the answer)
    • a comment in the code
    • the name of a file

    It lets the same code work with different data.

  3. Where can a local variable be used?
    • anywhere in the program
    • only inside the procedure or function where it is declared (the answer)
    • only in the main program
    • only in a file

    Outside that block it does not exist.

  4. Which keyword runs a procedure?
    • RUN
    • CALL (the answer)
    • DO
    • RETURN

    For example, CALL DrawLine(10).

  5. Which of these makes a program easier to maintain?
    • single-letter variable names
    • no comments
    • meaningful identifiers and comments (the answer)
    • one very long block of code

    Other programmers can then follow what the code does.

  6. A function body is RETURN Length * Width. It is called as Area(3, 6). What does it return?
    • 9
    • 18 (the answer)
    • 36
    • nothing

    3 × 6 = 18.

Still stuck on this one?Ask in the Papermunch Discord, or help someone else who is. Discord is for ages 13 and up.Join the server

Things you can type

Or go straight to

Or browse a shelf