AS Level · Topic 11.3
A-Level Computer Science 9618 / Topic 11 / AS

Write it once, call it often.

Structured programs are built from small named subroutines. A procedure is called as a statement of its own; a function returns a single value that replaces the call in an expression. Learn to define and call both, use the exact terminology examiners expect, and write efficient pseudocode.

The key idea

A procedure is a named block of code that performs a task; it may have none, one or more parameters and is run with a CALL statement. A function also performs a task but returns a single value: its header ends RETURNS data type, it uses RETURN, and it is used inside an expression, where the return value replaces the call. The values passed in a call are arguments; the identifiers in the header that receive them are parameters. Subroutines give modular, reusable code that is easier to test and maintain.

a procedure call is a statement; a function call is part of an expression main program Total ← 10 CALL ShowLine(5, '*') Total ← Total + Square(4) OUTPUT Total PROCEDURE ShowLine(...)builds and outputs *****ENDPROCEDURE FUNCTION Square(N ...)RETURN N * NENDFUNCTION returns control only 16 replaces the call: Total = 26
Fig. 1: control passes to the subroutine and back; only a function brings a value back with it
Section 01

Defining a procedure.

Use a procedure when a task is needed in more than one place, or to break a large problem into separate modules, and the task does not need to hand back a single value (it outputs something, or changes data). Step through the lab's procedure and function programs to watch control jump to the subroutine and back.

PROCEDURE ShowLine(Length : INTEGER, Symbol : CHAR)
    DECLARE Line : STRING
    DECLARE Count : INTEGER
    Line ← ""
    FOR Count ← 1 TO Length
        Line ← Line & Symbol
    NEXT Count
    OUTPUT Line
ENDPROCEDURE

PROCEDURE ShowBanner()
    CALL ShowLine(20, '=')
    OUTPUT "Weekly quiz results"
    CALL ShowLine(20, '=')
ENDPROCEDURE

CALL ShowBanner()
CALL ShowLine(5, '*')
Worked example Follow the calls
State exactly what the last two lines of the program above output.
  1. CALL ShowBanner() runs ShowBanner, which first calls ShowLine with Length 20 and Symbol '='.
  2. ShowLine joins '=' to Line 20 times and outputs a row of 20 = signs.
  3. ShowBanner then outputs the title and calls ShowLine(20, '=') again.
  4. Control returns to the main program: CALL ShowLine(5, '*') outputs *****.
Answer: A row of 20 = signs, the line Weekly quiz results, another row of 20 = signs, then *****
Your turn
Write the statement that outputs a row of 12 hyphens using ShowLine.
Examiner trap

A procedure does not return a value, so X ← ShowLine(5, '*') is wrong. If the main program needs a result back, write a function instead.

Section 02

Defining a function.

Use a function when the subroutine must work out one value for the code that calls it. A function call is not a complete statement: it is part of an expression, and when RETURN runs, the returned value replaces the call and the expression is then evaluated. CALL is never used with a function.

FUNCTION CountChar(Text : STRING, Target : CHAR) RETURNS INTEGER
    DECLARE Count : INTEGER
    DECLARE Index : INTEGER
    Count ← 0
    FOR Index ← 1 TO LENGTH(Text)
        IF MID(Text, Index, 1) = Target THEN
            Count ← Count + 1
        ENDIF
    NEXT Index
    RETURN Count
ENDFUNCTION

Total ← CountChar("banana", 'a') * 2
IF CountChar(Word, 'e') > 2 THEN
    OUTPUT "Lots of e"
ENDIF
Worked example Trace a function call
Trace Total ← CountChar("banana", 'a') * 2 and give the final value of Total.
  1. The function runs with Text = "banana", Target = 'a'; LENGTH(Text) is 6, so Index goes 1 to 6.
  2. Characters: b, a, n, a, n, a. Count goes up at Index 2, 4 and 6, so Count = 3.
  3. RETURN 3: the value 3 replaces the call, so the expression becomes 3 * 2.
  4. Total ← 6.
Answer:
IndexMID(Text, Index, 1)Count
0
1b0
2a1
3n1
4a2
5n2
6a3
RETURN 3, so Total = 6.
Your turn
What does CountChar("Mississippi", 's') return?
Examiner trap

CALL CountChar("banana", 'a') loses the mark: a function is never CALLed, and its return value would be thrown away.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which statement correctly runs the procedure ShowLine?

Quick check+10 XP

What is the main difference between a function and a procedure?

Quick check+10 XP

In Floor ← Area(4.0, 2.5), what are 4.0 and 2.5?

Quick check+10 XP

In the lab, PROCEDURE Stars(Number : INTEGER) is run by CALL Stars(3). What is Number?

Section 03

The terminology.

Questions often show a subroutine and ask you to identify its parts, so learn these words precisely. Then use subroutines and sensible loop choices to write efficient pseudocode.

TermMeaningIn FUNCTION Area(Length : REAL, Width : REAL) RETURNS REAL
Headerthe first line: the subroutine's name, its parameters and (for a function) the return typethe whole line
Interfacehow data passes in and out: the parameters with their data types and order, and the return typetwo REALs in, one REAL out
Parameteran identifier in the header that receives a value when the subroutine is calledLength, Width
Argumentthe actual value or variable passed in the call4.0 and 2.5 in Floor ← Area(4.0, 2.5)
Return valuethe single value a function sends back to replace the call10.0
// efficient: stops as soon as the item is found
FUNCTION FindItem(SearchItem : INTEGER) RETURNS INTEGER
    // Data is a global ARRAY[1:1000] OF INTEGER
    DECLARE Index : INTEGER
    Index ← 1
    WHILE Index <= 1000
        IF Data[Index] = SearchItem THEN
            RETURN Index
        ENDIF
        Index ← Index + 1
    ENDWHILE
    RETURN -1
ENDFUNCTION
Worked example Make it efficient
A program checks a list of 1000 values using a FOR loop from 1 to 1000 that sets a flag when the item is found, and the same 6 lines of output code are copied in three places. Suggest how to make the pseudocode more efficient.
  1. Replace the FOR loop with a conditional loop (or RETURN inside a function, as in FindItem) so that searching stops as soon as the item is found.
  2. If the item is at position 3, the efficient version makes 3 comparisons instead of 1000.
  3. Put the repeated output code into one procedure and CALL it three times: less code, and a change is made in one place.
  4. Also avoid working out the same value again inside a loop: calculate it once before the loop and store it in a variable.
Answer: Stop the loop early, and replace duplicated code with a procedure call
Your turn
In Floor ← Area(4.0, 2.5), identify the arguments and give the return value, assuming Area returns Length * Width.
Examiner trap

Do not mix up parameter and argument. The parameter is the name in the header (Length); the argument is what is passed in the call (4.0).

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

CountChar is a function returning an INTEGER. Which statement is NOT a valid use of it?

Exam style+20 XP

When is a function more appropriate than a procedure?

Exam style+20 XP

What is meant by the interface of a subroutine?

Exam style+20 XP

Square(N) returns N * N. Total is 10. What is Total after Total ← Total + Square(4)?

Skill unlocked

Procedures and functions, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Defining and calling procedures and functions in 9618 pseudocode, using the correct terminology, tracing function calls and writing efficient pseudocode.