8.1 Programming concepts

Show All Section Notes
Test yourself on Programming concepts 14 questions — drag-to-order, code completion, matching and multiple choice.
Start the quiz

Procedures, Functions and Variable Scope

Watch this lesson Video 8.1.4 · 8:18 · Subroutines, what they return, what they can see, and what they can change

1. Why Use Subroutines?

A subroutine is a named block of code that performs one specific task and can be called from anywhere in a program. Cambridge pseudocode has two kinds: procedures and functions.

Subroutines are used because they:

  • avoid repeating the same code in several places
  • make a program easier to read, test and maintain
  • allow one task to be written and tested independently of the rest of the program
  • let a large problem be broken down (decomposed) into smaller parts

2. Procedures vs Functions

This distinction is examined regularly, and the difference is exactly one thing: whether a value is returned.

 ProcedureFunction
Returns a value?NoYes — always returns exactly one value
KeywordsPROCEDURE … ENDPROCEDUREFUNCTION … RETURNS … ENDFUNCTION
How it is calledCALL Name(…)Used inside an expression, e.g. x ← Name(…)
Typical usePerform an action, e.g. display a menuCalculate and hand back a result, e.g. an area

A procedure

PROCEDURE DisplayMenu() OUTPUT "1. Add a record" OUTPUT "2. Delete a record" OUTPUT "3. Quit" ENDPROCEDURE // Called like this: CALL DisplayMenu()

A function

FUNCTION AreaOfRectangle(Width : INTEGER, Height : INTEGER) RETURNS INTEGER DECLARE Area : INTEGER Area ← Width * Height RETURN Area ENDFUNCTION // Called inside an expression: DECLARE MyArea : INTEGER MyArea ← AreaOfRectangle(5, 3) // MyArea is now 15

3. Parameters

A parameter is a value passed into a subroutine so that the same subroutine can work on different data each time it is called.

TermMeaning
ParameterThe variable named in the subroutine definition, e.g. Width
ArgumentThe actual value supplied when the subroutine is called, e.g. 5

A subroutine may take no parameters at all (as with DisplayMenu()), or several.

Syllabus limit: procedures and functions may have up to three parameters. You will never be asked to write one with more than three.
// A procedure with two parameters PROCEDURE PrintTimes(Message : STRING, Count : INTEGER) FOR i ← 1 TO Count OUTPUT Message NEXT i ENDPROCEDURE CALL PrintTimes("Hello", 3) // outputs Hello three times

3b. Passing Parameters: By Value and By Reference

When an argument is passed to a subroutine there are two ways it can travel, and the syllabus names both. The difference is whether the subroutine can change the caller's variable.

  • By value — a copy of the data is passed. The subroutine works on the copy, so any change it makes is discarded when the subroutine ends. The original variable is untouched.
  • By reference — the location of the original variable is passed. The subroutine works on the original itself, so any change it makes persists after the subroutine ends.
By value By reference
What is passed A copy of the value The address of the original
Original changed? No Yes
Use it when The subroutine only needs to read the value The subroutine must update the caller's variable
Worked contrast. A variable Score holds 10 and is passed to a subroutine that doubles its parameter. Passed by value, the subroutine's copy becomes 20, and after the call Score is still 10. Passed by reference, the subroutine changes the original, and after the call Score is 20. Same subroutine, same argument, different result — and the only difference is how the parameter was passed.

Why by value is the safer default: a subroutine that cannot alter the caller's variables cannot cause an unexpected change somewhere else in the program. Passing by reference is more efficient for large data such as an array, since nothing is copied, but it gives the subroutine the power to modify data its caller may not expect to change.

4. Local and Global Variables

Where a variable is declared decides which parts of the program can see and use it. This is called the variable's scope.

 Local variableGlobal variable
DeclaredInside a subroutineOutside every subroutine, in the main program
Can be used byOnly that subroutineThe whole program, including all subroutines
ExistsOnly while the subroutine is runningFor as long as the program runs
DECLARE Total : INTEGER // GLOBAL - visible everywhere Total ← 0 PROCEDURE AddScore(Score : INTEGER) DECLARE Bonus : INTEGER // LOCAL - only exists in here Bonus ← 10 Total ← Total + Score + Bonus ENDPROCEDURE CALL AddScore(5) OUTPUT Total // 15 - Total is global, so this works // OUTPUT Bonus would FAIL - Bonus is local to AddScore

Why local variables are preferred

  • They cannot be changed accidentally by another part of the program.
  • The same variable name can be reused safely in different subroutines.
  • They free up memory when the subroutine finishes.
  • A subroutine using only local variables and parameters can be tested on its own.

5. Nested Statements

A statement is nested when it is placed inside another statement of the same kind — a loop inside a loop, or an IF inside an IF.

// Nested iteration: printing a 3 x 4 grid of stars FOR Row ← 1 TO 3 FOR Column ← 1 TO 4 OUTPUT "*" NEXT Column NEXT Row // Nested selection IF Age >= 13 THEN IF Age <= 19 THEN OUTPUT "Teenager" ENDIF ENDIF
Syllabus limit: you will not be required to write more than three levels of nested statements.

The inner loop completes in full for every single pass of the outer loop. In the grid example the outer loop runs 3 times and the inner loop runs 4 times per pass, so OUTPUT happens 3 × 4 = 12 times.

6. Exam Focus

The difference is the return value, not the length or complexity. Candidates often write that "a function is bigger" or "a procedure is simpler". Neither earns the mark. A function returns a value; a procedure does not.
Call them correctly. A procedure is called with CALL. A function is not — it appears inside an expression, because its returned value has to go somewhere. Writing CALL AreaOfRectangle(5, 3) as a standalone statement discards the result and loses marks.
Keep parameter and argument straight. The parameter is in the definition; the argument is the value passed in the call. Also match them in order and in number — a subroutine defined with two parameters must be called with two arguments.
Trace nested loops carefully. When completing a trace table for nested iteration, finish the entire inner loop before advancing the outer loop counter. Advancing both together is the single most common error in nested-loop trace questions.

Quick self-check

  • State the one difference between a procedure and a function.
  • Write a function that takes two parameters and returns the larger of them.
  • Explain why Bonus in the example above cannot be output from the main program.
  • State the maximum number of parameters a subroutine may have in this syllabus.
  • How many times does OUTPUT run if the outer loop is 1 to 5 and the inner is 1 to 3?