SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
CAIE 9618 · Paper 2 · Topic 2.4.2

Functions &
Procedures

PROCEDURE vs FUNCTION · Parameters · Built-in Functions · Libraries

CSZone Cambridge International AS & A Level Computer Science 9618
Procedures vs Functions

The Key Distinction

PROCEDURE — no return value
PROCEDURE greet(name:STRING)
OUTPUT "Hello, " & name
ENDPROCEDURE

// Call:
CALL greet("Ali")
FUNCTION — returns a value
FUNCTION square(n:INTEGER):INTEGER
RETURN n * n
ENDFUNCTION

// Call:
result <- square(5) // = 25
PROCEDUREFUNCTION
Returns value?NoYes (with RETURN)
Return type declared?NoYes (after colon)
Called with?CALL keywordIn expression/assignment
Side effects?Typically yes (output/update)Typically no (pure)
Built-in Functions

CAIE Pseudocode Standard Library

MATHS
ROUND(3.7, 0) // 4.0
INT(7.9) // 7 (truncate)
SQR(16) // 4 (square root)
RANDOM() // 0.0 to 1.0
STRING FUNCTIONS
LENGTH("hello") // 5
UCASE("abc") // "ABC"
LCASE("ABC") // "abc"
MID("hello",2,3) // "ell"
LEFT("hello",3) // "hel"
RIGHT("hello",3) // "llo"
CONVERSION
STR(42) // "42"
INT("99") // 99
CHR(65) // 'A'
ASC('A') // 65
NUM_TO_STR(3.14) // "3.14"
DATE/TIME (if required)
TODAY() // current date
NOW() // current date+time
Library Routines

Pre-written, Reusable Code

A library routine is a pre-written, tested, and reusable piece of code (procedure or function) stored in a library file. Programs can import and use these routines without rewriting them.
BENEFITS
Saves development time — no reinventing the wheel. Pre-tested and reliable. Consistent behaviour. Can be updated independently. Share code across multiple programs.
TYPES
Static library — code copied into executable at compile time. Larger file but no dependency.
Dynamic library — linked at runtime (DLL/shared object). Smaller executable but requires the library at runtime.
Exam Practice

Cambridge-style questions

Question 1
Write a FUNCTION called validate that takes an integer parameter score and returns TRUE if score is in the range 0–100 (inclusive), or FALSE otherwise.
4 marks
FUNCTION validate(score : INTEGER) : BOOLEAN // 1 mark — correct header with return type
IF score >= 0 AND score <= 100 THEN // 1 mark — correct condition
RETURN TRUE // 1 mark
ELSE
RETURN FALSE // 1 mark
ENDIF
ENDFUNCTION
Common Mistakes

Don't lose easy marks

1
Using CALL with a function — procedures are called with CALL. Functions are called by using them in an expression (e.g. result ← square(5)). Writing "CALL square(5)" is wrong — the return value goes nowhere.
2
Forgetting the return type in a function header — CAIE requires FUNCTION name(params) : RETURN_TYPE. Missing the colon and type loses marks. Procedures don't have this — that's a key difference.
3
Confusing MID indexing — MID(s, 2, 3) returns 3 characters starting at position 2 (not 3). The second argument is the starting position, not the end position. This trips up candidates who think like Python's slicing.
Topic Summary — 2.4.2

What You Need to Know

PROCEDURE vs FUNCTION
PROCEDURE: no RETURN, called with CALL
FUNCTION: has RETURN + return type
Function used in expressions/assignments
BUILT-IN FUNCTIONS
Maths: ROUND, INT, SQR, RANDOM
Strings: LENGTH, MID, LEFT, RIGHT, UCASE, LCASE
Conversion: STR, INT, CHR, ASC
LIBRARY ROUTINES
Pre-written, tested, reusable code
Static: copied at compile time
Dynamic (DLL): linked at runtime
Benefits: saves time, reliable, shareable
CSZone

Next Video

2.4.3
File Handling & Exception Handling
OPEN · READ · WRITE · CLOSE · TRY-EXCEPT
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools