Learn
8.1.6A | PROCEDURES, FUNCTIONS AND PARAMETERS
01 | WHY GIVE A TASK ITS OWN NAME?
Imagine a quiz that greets a player, checks answers and calculates a final score. If the same greeting is needed in several places, copying its instructions everywhere makes the program longer and harder to update.
Instead, give that task a name and define its instructions once. Other parts of the program can ask that named task to run whenever needed. A routine, also called a subroutine, is a named group of instructions that performs a particular task.
Procedures and functions are two kinds of routine. Both help divide a larger program into understandable parts. Their key difference is whether they return a value for the calling expression to use.
02 | A PROCEDURE PERFORMS A TASK
A procedure is a named routine that performs a task without returning a value to replace its call in an expression. Its task may display a message, draw something or carry out a sequence of actions.
The example defines a procedure called ShowWelcome. Its body is the instruction or group of instructions between PROCEDURE and ENDPROCEDURE. Here the body contains one OUTPUT instruction.
A definition states what the procedure will do. It does not, by itself, output the welcome message. Another instruction must call the procedure.
PROCEDURE ShowWelcome OUTPUT "Welcome to the quiz" ENDPROCEDURE
03 | A CALL ASKS THE PROCEDURE TO RUN
A call is an instruction or expression that starts a routine. The caller is the part of the program making that call. In Cambridge pseudocode, a procedure is called using CALL.
Execution reaches CALL ShowWelcome, runs the procedure’s body and then continues at the instruction after that call. The outputs are “Before greeting”, “Welcome to the quiz” and “After greeting”, in that order.
The definitions are placed at the start of the code. The main program contains the instructions that coordinate the overall task, including calls to those routines.
PROCEDURE ShowWelcome OUTPUT "Welcome to the quiz" ENDPROCEDURE OUTPUT "Before greeting" CALL ShowWelcome OUTPUT "After greeting"
04 | PARAMETERS MAKE THE SAME TASK FLEXIBLE
A fixed welcome message is useful, but a personalised greeting is better. A parameter is a named item in a routine’s definition that receives a value supplied when the routine is called.
PlayerName below is a STRING parameter. The procedure uses it in its body, so the same instructions can greet different people.
At the first call, PlayerName receives “Maya”. At the second, it receives “Leo”. The outputs are “Hello, Maya” and “Hello, Leo”. We have reused one task with different supplied values.
PROCEDURE Greet(PlayerName : STRING)
OUTPUT "Hello, ", PlayerName
ENDPROCEDURE
CALL Greet("Maya")
CALL Greet("Leo")05 | PARAMETER NAME AND SUPPLIED VALUE
The parameter is the name written in the definition. The actual value or expression supplied in the call is often called an argument. Cambridge examples also describe these as parameter values. Understanding their roles matters more than treating the terms as rival labels.
In CALL Greet("Maya"), PlayerName is the parameter and “Maya” supplies its value. A caller can supply a variable too: CALL Greet(StudentName) uses the current value of StudentName. The names do not need to match.
Do not confuse a parameter with user input. A parameter receives information from the caller. INPUT receives information from outside the program, such as the user. A routine can use either mechanism, but they are different operations.
| Location | Item | Role |
|---|---|---|
| Definition | PlayerName : STRING | Names the parameter and its expected type |
| Call | "Maya" | Supplies a STRING value |
| Call | StudentName | Supplies that variable’s current value |
06 | MATCH NUMBER, ORDER AND TYPE
A procedure can have several parameters. Their order in the definition determines which supplied value belongs to each name. Their declared types tell the caller what kinds of values to supply.
For this example, the first supplied value is a STRING name and the second is an INTEGER number of points. CALL ShowScore("Maya", 12) displays “Maya scored 12”. Reversing those arguments would put the wrong types in the positions.
The syllabus allows procedures and functions with or without parameters, with up to three parameters. Use only the information a routine needs.
PROCEDURE ShowScore(PlayerName : STRING, Points : INTEGER)
OUTPUT PlayerName, " scored ", Points
ENDPROCEDURE
CALL ShowScore("Maya", 12)07 | A FUNCTION RETURNS A VALUE
A function is a named routine that returns a single value to the point where it is called. Its call can therefore be used as part of an expression, such as a calculation, assignment or output expression.
The function Double takes an INTEGER parameter and returns an INTEGER result. RETURNS INTEGER in its header states the result’s type. RETURN in the body supplies the actual result.
When Double(6) is evaluated, Number receives six, the body calculates twelve and RETURN sends twelve back. The calling expression then uses twelve in place of Double(6).
FUNCTION Double(Number : INTEGER) RETURNS INTEGER RETURN Number * 2 ENDFUNCTION DECLARE Result : INTEGER Result ← Double(6) OUTPUT Result
08 | RETURN IS NOT OUTPUT
OUTPUT communicates information to the user. RETURN supplies a function’s result to its caller. A calculation can return a result without displaying anything.
In Result ← Double(6), twelve is stored in Result. No screen output is caused by that assignment. A later OUTPUT Result displays it.
A procedure that displays twelve has performed an output task, but it has not supplied twelve as a function result for another calculation. This distinction is important when deciding whether a routine should be a procedure or function.
| Instruction | Effect |
|---|---|
| RETURN Number * 2 | Returns the calculated result to the calling expression |
| Result ← Double(6) | Stores the returned value in Result |
| OUTPUT Result | Displays the value currently held in Result |
09 | USE A FUNCTION INSIDE AN EXPRESSION
A returned value can be used immediately rather than stored first. In Total ← Double(6) + 3, the function call supplies twelve, then the caller adds three and stores fifteen.
In Cambridge pseudocode, do not use CALL for a function. A function call belongs in an expression; CALL is used for a procedure as a complete statement.
Function results are not limited to numbers. A function can return BOOLEAN for a check or STRING for generated text, provided its returned value matches its declared result type.
Total ← Double(6) + 3 OUTPUT Double(4) // First assignment stores 15 // OUTPUT displays 8
10 | COMPARE THE TWO KINDS OF ROUTINE
Ask what the caller needs. If it needs an action performed, such as displaying a greeting, use a procedure. If it needs a value to use, such as a calculated price, use a function.
Both can receive parameters, and both have definitions and bodies. The difference is not that procedures are always simple or functions always mathematical. It is the returned value and how the call is used.
| Feature | Procedure | Function |
|---|---|---|
| Purpose | Perform a task | Produce a value for the caller |
| Cambridge call | CALL Greet("Maya") | Cost ← TicketCost(3) |
| Value returned to calling expression | No function result | One value |
| Parameters | May have none or several | May have none or several |
| Result type in header | No RETURNS result type | RETURNS states the result type |
11 | A TICKET CALCULATION WITH TWO PARAMETERS
To calculate ticket cost, the function needs the number of tickets and the price of each. Quantity is INTEGER, Price is REAL and the returned cost is REAL.
TicketCost(3, 4.50) assigns three to Quantity and 4.50 to Price for that call. The body multiplies them and returns 13.5. This is a numeric value; displaying it as 13.50 would be a separate formatting choice.
The function has no INPUT or OUTPUT instruction. Its caller supplies the information and decides what to do with the result.
FUNCTION TicketCost(Quantity : INTEGER, Price : REAL) RETURNS REAL RETURN Quantity * Price ENDFUNCTION DECLARE Cost : REAL Cost ← TicketCost(3, 4.50) OUTPUT "Cost: ", Cost
12 | STEP THROUGH A FUNCTION CALL
Choose the quantity and price, then follow the call. The demo separates the supplied arguments, the parameter values, the returned result and the caller’s final assignment.
These controls accept a non-negative whole-number quantity and a non-negative price. They validate the demo’s input; the simple pseudocode function above assumes suitable values have already been supplied. Price uses two decimal places here.
13 | THE MAIN PROGRAM CONNECTS THE PARTS
Procedures and functions work together. This complete example asks the user for a name and quantity, greets them with a procedure, calculates the cost with a function and outputs the result.
Greet receives a value but does not return one. TicketCost receives two values and returns a calculated cost. The main program controls the input and output sequence. Assume the quantity supplied is a valid non-negative INTEGER.
PROCEDURE Greet(PlayerName : STRING) OUTPUT "Hello, ", PlayerName ENDPROCEDURE FUNCTION TicketCost(Quantity : INTEGER, Price : REAL) RETURNS REAL RETURN Quantity * Price ENDFUNCTION DECLARE Name : STRING DECLARE Tickets : INTEGER DECLARE Cost : REAL OUTPUT "Enter your name" INPUT Name OUTPUT "Enter ticket quantity" INPUT Tickets CALL Greet(Name) Cost ← TicketCost(Tickets, 4.50) OUTPUT "Cost: ", Cost
14 | WHY THIS ORGANISATION HELPS
Reuse: TicketCost can calculate the cost at several points without copying its calculation. Different arguments allow the same routine to handle different quantities and prices.
Readability: a meaningful name communicates a task’s purpose. A reader can understand the main program’s sequence before inspecting every instruction inside its routines.
Maintenance and testing: a calculation can be updated in its definition and checked with suitable examples. This reduces duplicated logic, but it does not automatically prove the program correct. Changed behaviour must still suit every caller.
Passing explicit parameters makes the routine’s required information visible. Variable scope and how changes to variables behave are explored in the later local-and-global-variables lesson; do not assume a routine automatically changes the caller’s variables.
15 | AVOID THE COMMON MIX-UPS
A definition describes a routine; a call runs it. A parameter names incoming information; an argument supplies its value. RETURN sends a function result to the caller; OUTPUT displays information.
Do not write CALL TicketCost(3, 4.50) when using this function. Write a call in an expression, such as Cost ← TicketCost(3, 4.50). Do not try to assign a value from a procedure that returns no function result.
Match argument count, order and types to the definition. Do not confuse RETURNS, which states the type in a function header, with RETURN, which supplies the result from its body.
You should now be able to explain the three core ideas. The next two lessons develop defining and calling procedures, then defining and calling functions, in more detail.