← IGCSE Computer Science Paper 2

Writing a short program with a procedure and a function

The section after all procedure and function definitions. It calls them in order, using CALL for procedures and function calls inside expressions or OUTPUT.

Understanding Writing a short program with a procedure and a function in IGCSE Computer Science Paper 2

What it does: The definition block comes first, so the main program below can call ShowMenu. The main program calls the function in an assignment and then outputs the stored result, in order. A procedure is called with CALL on its own line. It does not return a value, so it cannot be stored in a variable. A function call can sit inside OUTPUT because it produces a value. CALL is only used for procedures. The call must give one argument for each of the three parameters, in the right order. Move the PROCEDURE definition above the CALL so the definitions come at the start.

Common mistakes: A procedure does not send a value back, so it cannot use RETURN. It should show the text with OUTPUT "Quiz Time". A function must send its answer back to the main program with RETURN. It should be RETURN N * N, so that OUTPUT Sq(5) can show the result.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 419–428) and word search (Modules 419–438).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Where do the procedure and function definitions go in a complete program?
At the start, before the main programAfter the main program, at the very end
Matches
CALL ShowMenu() PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE
↓
The main program calls the procedure before it has been defined. Definitions must be listed first.
Applying
Meanings
Term
main program
Meaning
The section after all procedure and function definitions. It calls them in order, using CALL for procedures and function calls inside expressions or OUTPUT.
Practice
PROCEDURE ShowTotal(Total : INTEGER)
Testing
Gaps
ShowTitle()
Check Answer
Spots
1PROCEDURE ShowTitle()
2 RETURN "Quiz Time"

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Dolphin' badge — Reef Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Where do the procedure and function definitions go in a complete program?
A. At the start, before the main program ✓
B. After the main program, at the very end
Definitions are written first. The main program comes after them and calls them in order.
2. Which line in the main program calls a procedure named ShowMenu?
A. CALL ShowMenu() ✓
B. total ← ShowMenu()
A procedure is called with CALL on its own line. It does not return a value, so it cannot be stored in a variable.
3. Which main program line calls a function named AddThree that takes three parameters?
A. total ← AddThree(a, b, c) ✓
B. CALL AddThree(a, b, c)
A function returns a value, so its call is used in an expression, such as an assignment. The result is stored in total.
4. Which is a sensible order for the main program of a program split across subroutines?
A. OUTPUT, then calculate, then INPUT
B. INPUT, then calculate, then OUTPUT ✓
The main program calls the subroutines in a logical order. Get the data first, process it, then display the result.
5. Which main program line uses a function call directly inside OUTPUT?
A. OUTPUT CALL AddThree(4, 5, 6)
B. OUTPUT AddThree(4, 5, 6) ✓
A function call can sit inside OUTPUT because it produces a value. CALL is only used for procedures.

Match the Pairs

CALL ShowMenu() → In the main program, this line runs the procedure ShowMenu, then control returns to the next line
OUTPUT AddThree(2, 5, 8) → In the main program, this calls the function AddThree with three values and outputs the value it returns
Procedures and functions are defined at the start of the program, before the main program uses them → PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE
The main program stores a function result in a variable, then uses it in a later step → Total ← AddThree(A, B, C) OUTPUT Total
CALL ShowMenu() PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE → The main program calls the procedure before it has been defined. Definitions must be listed first.
CALL AddThree(2, 5, 8) → AddThree is a function, so it cannot be used with CALL. Its returned value must be used in an expression or OUTPUT.

Key Term

main program
The section after all procedure and function definitions. It calls them in order, using CALL for procedures and function calls inside expressions or OUTPUT.

Fill in the Blanks

1. PROCEDURE ShowTitle() OUTPUT "Area Calculator" ENDPROCEDURE
Hint: Type the keyword that starts a procedure definition, placed before the main program.
2. FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Hint: Type the keyword that introduces the data type a function sends back.
3. CALL ShowTitle() INPUT Side OUTPUT Square(Side)
Hint: Type the keyword the main program uses to run a procedure.
4. PROCEDURE ShowSum(A, B, C : INTEGER) OUTPUT A + B + C ENDPROCEDURE
Hint: Type the name of the third parameter so all three values are added.
5. INPUT Side Area ← Square(Side) OUTPUT Area
Hint: Type the keyword that displays the stored function result at the end of the main program.

Spot the Error

Question 1
A PROCEDURE ShowTitle()
B RETURN "Quiz Time"
C ENDPROCEDURE
D CALL ShowTitle()
Line B — A procedure does not send a value back, so it cannot use RETURN. It should show the text with OUTPUT "Quiz Time".
Question 2
A FUNCTION Sq(N) RETURNS INTEGER
B OUTPUT N * N
C ENDFUNCTION
D OUTPUT Sq(5)
Line B — A function must send its answer back to the main program with RETURN. It should be RETURN N * N, so that OUTPUT Sq(5) can show the result.
Question 3
A PROCEDURE Greet()
B OUTPUT "Hello"
C ENDFUNCTION
D CALL Greet()
Line C — A procedure definition must end with ENDPROCEDURE, not ENDFUNCTION. The closing keyword has to match the opening keyword.
Question 4
A FUNCTION Half(N) RETURNS REAL
B Ans <- N / 2
C RETURN Total
D ENDFUNCTION
E OUTPUT Half(9)
Line C — Total was never given a value in this function. The calculated value is stored in Ans, so the line should be RETURN Ans.
Question 5
A FUNCTION Show(N : INTEGER)
B OUTPUT "Score: ", N
C ENDPROCEDURE
D CALL Show(10)
Line A — This subroutine only outputs and has no RETURN. It is a procedure, and it ends with ENDPROCEDURE, so the first line must start with PROCEDURE.

Student with a class code? Join your class →