🎯 Syllabus & Goals 3 min
Cambridge 8.1.6 · Procedures and functions Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Define and call a procedure, with and without parameters, in pseudocode and Python.
- Define a function that uses
RETURN, and call it on the right-hand side of an expression. - Explain the difference between a procedure and a function, and between a parameter and an argument.
Textbook: Chapter 8, §8.1.6 (pp. 321–325)
Recap / Warm-Up 5 min
Last lesson: nesting — loops and selection inside each other. Programs are getting long. Today you learn to package code into named blocks that can be re-used.
Quick starter
You have already used ready-made routines. What value does LENGTH("Robot") give back — and did you need to know how it works inside?
Reveal the answer
5. No — you only needed its name, what to pass in and what it returns. That is exactly how you will use your own functions.
🧠 Key Concept 14 min
Programs often repeat the same group of statements. Instead of copying them each time, you define them once as a subroutine — a named procedure or function — and call it whenever it is needed.
1 · Procedures — with and without parameters
Cambridge pseudocode
PROCEDURE Stars
OUTPUT "************"
ENDPROCEDURE
PROCEDURE StarLine(Number : INTEGER)
DECLARE Line : STRING
DECLARE Counter : INTEGER
Line ← ""
FOR Counter ← 1 TO Number
Line ← Line & "*"
NEXT Counter
OUTPUT Line
ENDPROCEDURE
CALL Stars
CALL StarLine(7)The same in Python
def stars(): print("************") def star_line(number): for counter in range(number): print("*", end="") # end="" keeps the stars on one line print() # then finish the line stars() star_line(7)
************ *******
A procedure call is a standalone statement: CALL StarLine(7). The value 7 is the argument; inside the procedure it is stored in the parameter Number. Python calls a procedure a void function.
2 · Functions — they RETURN a value
FUNCTION Celsius(Temperature : REAL) RETURNS REAL RETURN (Temperature - 32) / 1.8 ENDFUNCTION MyTemp ← Celsius(212)
def celsius(temperature): return (temperature - 32) / 1.8 my_temp = celsius(212) print(my_temp)
100.0
CALL.3 · The header line
The first line of a definition is its header. It contains the name, each parameter with its data type, and — for a function only — the data type of the value returned.
| Procedure | Function | |
|---|---|---|
| Header | PROCEDURE Name(P : TYPE) | FUNCTION Name(P : TYPE) RETURNS TYPE |
| Ends with | ENDPROCEDURE | ENDFUNCTION |
| Returns a value? | No | Yes — using RETURN |
| How it is called | Standalone: CALL Name(Arg) | In an expression: X ← Name(Arg) |
| Python | def with no return value (void function) | def with return (fruitful function) |
ConvertCurrency(Amount, Rate) function used on dozens of screens. When the rounding rule changed, the developers fixed one function instead of dozens of copies. A game calls a PlaySound(Name) procedure every time something happens — it does a job but gives nothing back.Worked Example 12 min
Example 1 · Procedures with zero and two parameters
A scoreboard program prints a banner, a title and lines of different lengths and symbols.
Cambridge pseudocode
PROCEDURE Banner
OUTPUT "=============="
ENDPROCEDURE
PROCEDURE DrawLine(Length : INTEGER, Symbol : CHAR)
DECLARE Line : STRING
DECLARE Counter : INTEGER
Line ← ""
FOR Counter ← 1 TO Length
Line ← Line & Symbol
NEXT Counter
OUTPUT Line
ENDPROCEDURE
CALL Banner
OUTPUT " SCORE BOARD"
CALL Banner
CALL DrawLine(5, '*')
CALL DrawLine(3, '-')The same in Python
# A procedure with no parameters, then one with two parameters def banner(): print("==============") def draw_line(length, symbol): line = "" for counter in range(length): line = line + symbol print(line) banner() print(" SCORE BOARD") banner() draw_line(5, "*") draw_line(3, "-")
============== SCORE BOARD ============== ***** ---
- The definitions come first but do nothing until called.defining a subroutine is not the same as running it.
CALL DrawLine(5, '*')passes 5 intoLengthand'*'intoSymbol.arguments match parameters by position.- The loop builds
"*****", outputs it, and control returns to the next call.the second call re-uses the same code with different data.
Example 2 · A function with two parameters
A flooring company works out the total area of two rooms, then the cost at $12.50 per square metre.
Cambridge pseudocode
CONSTANT PricePerSqMetre ← 12.50 DECLARE Kitchen, Lounge, TotalArea : REAL FUNCTION RectangleArea(Length : REAL, Width : REAL) RETURNS REAL RETURN Length * Width ENDFUNCTION Kitchen ← RectangleArea(4, 3.5) Lounge ← RectangleArea(5, 4) TotalArea ← Kitchen + Lounge OUTPUT "Total floor area: ", TotalArea, " square metres" OUTPUT "Flooring cost: $", TotalArea * PricePerSqMetre
The same in Python
# A function with two parameters returns one value PRICE_PER_SQ_METRE = 12.50 def rectangle_area(length, width): return length * width kitchen = rectangle_area(4, 3.5) lounge = rectangle_area(5, 4) total_area = kitchen + lounge print("Total floor area:", total_area, "square metres") print("Flooring cost: $", total_area * PRICE_PER_SQ_METRE)
Total floor area: 34.0 square metres Flooring cost: $ 425.0
| Call | Length | Width | Returned | Kitchen | Lounge | TotalArea | OUTPUT |
|---|---|---|---|---|---|---|---|
| RectangleArea(4, 3.5) | 4 | 3.5 | 14.0 | 14.0 | |||
| RectangleArea(5, 4) | 5 | 4 | 20.0 | 20.0 | |||
| 34.0 | Total floor area: 34.0 square metres | ||||||
| Flooring cost: $425.0 |
How the marks are earned when you write a function: correct header with parameters and types (1), RETURNS with the type (1), a correct RETURN statement (1), ENDFUNCTION (1), and a call used in an expression (1).
Try It Yourself 12 min
Goal: Define a procedure Greeting with no parameters that outputs a two-line welcome message. Call it three times.
Goal: Write a function Fahrenheit(Temperature : REAL) RETURNS REAL that converts Celsius to Fahrenheit (× 1.8 + 32). Use it in a program that inputs a Celsius value and outputs the result. Test with 0, 100 and 37.
Goal: Write a function Larger(A : INTEGER, B : INTEGER) RETURNS INTEGER. Then use it — without any extra IF statements — to find the largest of three input numbers.
Hint
A function call can be an argument to another call: Largest ← Larger(Larger(X, Y), Z). The inner call is worked out first.
📝 Exam Practice 10 min
Explain the difference between a procedure and a function.
Mark scheme
- Both are named sets of statements that can be called to perform a task (1).
- A function returns a value to the calling program; a procedure does not (1).
- A function is called as part of an expression / on the right-hand side of an assignment; a procedure call is a standalone statement (1).
State three items that appear in the header of a function definition.
Mark scheme
- The name of the function (1).
- Any parameters and their data types (1).
- The data type of the return value (1).
Write a pseudocode function Average that takes two whole numbers as parameters and returns their average. Show how it would be called to store the average of 7 and 10 in the variable Result.
Mark scheme
FUNCTION Average(Num1 : INTEGER, Num2 : INTEGER)(1)RETURNS REAL(1)RETURN (Num1 + Num2) / 2— with brackets (1)ENDFUNCTION(1)Result ← Average(7, 10)(1)
In CALL DrawLine(5, '*') and PROCEDURE DrawLine(Length : INTEGER, Symbol : CHAR), identify one argument and one parameter.
Mark scheme
- Argument:
5or'*'(1). - Parameter:
LengthorSymbol(1).
🗝️ Recap & Key Terms 3 min
Define once, call many times. A procedure does a job and is called with CALL. A function does a job and RETURNs one value, so it is used inside an expression. Arguments in the call fill the parameters in the header — at most two at IGCSE.
- Procedure
- A set of programming statements grouped under a single name that can be called to perform a task, rather than including a copy of the code every time.
- Function
- Like a procedure, except that it returns a value back to the program that called it.
- Parameter
- A variable in a procedure or function declaration that stores the value of an argument passed to it.
- Argument
- The actual value passed to a procedure or function when it is called.
- RETURN
- The keyword in a function that specifies the value sent back to the calling expression.
Homework 1 min
Task (≤ 15 min): Write a pseudocode function IsEven(Number : INTEGER) RETURNS BOOLEAN. Then write a FOR loop that uses it to output only the even numbers from 1 to 20. [5]
Model answer
FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN
IF MOD(Number, 2) = 0
THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
DECLARE Counter : INTEGER
FOR Counter ← 1 TO 20
IF IsEven(Counter) = TRUE
THEN
OUTPUT Counter
ENDIF
NEXT CounterMarks: header with parameter and RETURNS BOOLEAN (1); MOD test (1); both RETURN values (1); loop 1 to 20 (1); function called inside the IF condition (1).