Cambridge IGCSE · Topic 8.1
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Write it once.

Large programs are built from small named blocks of code. Learn how to define and call procedures and functions in 0478 pseudocode, pass values in as parameters, return a value from a function, and tell local variables from global ones.

The key idea

A procedure is a named block of code that performs a task; it is run with CALL as a statement on its own and does not return a value. A function is a named block of code that returns a single value, so it is used inside an expression; its header states the data type it RETURNS. Parameters are the variables in the header that receive the values passed in when it is called (up to three in the exam). A local variable is declared inside a subroutine and can only be used there; a global variable is declared in the main program and can be used anywhere.

main program CALL Stars(3)OUTPUT "Done"X ← Square(4)OUTPUT X PROCEDURE Stars runs, returns no value FUNCTION Square RETURNS INTEGER control returns 16 returned a procedure is CALLed; a function is used inside an expression
Fig. 1: control passes to the subroutine and back; a function also hands back a value
Section 01

Procedures and CALL.

In the lab, choose Procedure with a parameter and step through it. The call stack shows when control has passed into the procedure and when it comes back to the main program.

PROCEDURE Stars(Number : INTEGER)
    DECLARE Count : INTEGER
    FOR Count ← 1 TO Number
        OUTPUT "*"
    NEXT Count
ENDPROCEDURE

CALL Stars(3)
OUTPUT "Done"

The same idea in Python (for comparison only: the exam uses pseudocode)

def stars(number):
    for count in range(number):
        print("*")

stars(3)
print("Done")
Worked example Two calls
Using the procedure above, the main program is CALL Stars(2) then CALL Stars(1). How many stars are output?
  1. The first call sets Number to 2: the loop outputs 2 stars.
  2. Control returns, then the second call sets Number to 1: 1 star.
Answer: 3 stars
Examiner trap

A procedure with no parameters is still called with CALL, e.g. CALL DrawLine. Do not write a procedure call inside an expression: it has no value to give back.

Section 02

Functions return a value.

A function works like a procedure, but it hands a single value back to the place where it was called. The RETURN statement sets that value.

FUNCTION SumSquare(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
    RETURN Number1 * Number1 + Number2 * Number2
ENDFUNCTION

OUTPUT "Sum of squares = ", SumSquare(10, 20)

SumSquare(10, 20) returns 100 + 400 = 500, so the output is Sum of squares = 500.

ProcedureFunction
Returns a value?NoYes, exactly one
HeaderPROCEDURE Name(...)FUNCTION Name(...) RETURNS type
How it is calledCALL Name(...) as a statementinside an expression, e.g. X ← Name(...)

The same idea in Python (for comparison only: the exam uses pseudocode)

def sum_square(number1, number2):
    return number1 * number1 + number2 * number2

print("Sum of squares =", sum_square(10, 20))
Worked example A function with IF
FUNCTION Larger(A : INTEGER, B : INTEGER) RETURNS INTEGER returns A if A > B, otherwise B. What is output by OUTPUT Larger(4, 9) + Larger(7, 2)?
  1. Larger(4, 9): 4 > 9 is FALSE, so it returns 9.
  2. Larger(7, 2): 7 > 2 is TRUE, so it returns 7.
  3. The returned values replace the calls: 9 + 7.
Answer: 16
Examiner trap

Never use CALL with a function. Its value must be used: assigned, output or part of a calculation, e.g. Total ← SumSquare(2, 3).

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

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

Quick check+10 XP

Which statement correctly runs the procedure Stars so it outputs 4 stars?

Quick check+10 XP

In the lab's Local and global variables program, what is output for the global Total?

Quick check+10 XP

What is a parameter?

Section 03

Local and global variables.

The lab's Local and global variables program shows the same idea: the procedure has its own Total, so the global Total is not changed. Here is a similar program.

PROCEDURE AddBonus
    DECLARE Total : INTEGER     // local: only exists inside AddBonus
    Total ← 5
    OUTPUT "Inside: ", Total
ENDPROCEDURE

DECLARE Total : INTEGER         // global: declared in the main program
Total ← 100
CALL AddBonus
OUTPUT "Outside: ", Total

The output is Inside: 5 then Outside: 100. The local Total is a separate variable that disappears when the procedure ends.

Worked example A procedure that uses a global
PROCEDURE AddTen contains only Score ← Score + 10, with no local declaration. The main program declares Score globally, sets Score ← 5, then calls AddTen twice and outputs Score. What is output?
  1. Score is global, so AddTen changes the main program's Score.
  2. First call: 5 + 10 = 15.
  3. Second call: 15 + 10 = 25.
Answer: 25
Examiner trap

The lab program outputs Local Total 6 and then Global Total 10: assigning to a local variable with the same name never touches the global one.

Stage 2 · Exam

Exam-style questions

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

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

Using the SumSquare function in this lesson, what does SumSquare(3, 4) return?

Exam style+20 XP

Which line is a correct use of the function SumSquare?

Exam style+20 XP

Which is a benefit of using local variables in a procedure?

Exam style+20 XP

Which is a correct function header in 0478 pseudocode?

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, parameters, return values, and local and global variables.