What you'll learn
Procedures and functions are fundamental building blocks of structured programming that you must understand for your CIE IGCSE Computer Science exam. This guide explains how to use subroutines effectively, pass data into them using parameters, and return values from functions. You'll learn the key differences between procedures and functions, and how to apply these concepts in pseudocode and programming languages.
Key terms and definitions
Procedure — a named block of code that performs a specific task but does not return a value to the main program.
Function — a named block of code that performs a specific task and returns a single value to the point where it was called.
Parameter — a variable declared in the subroutine definition that receives data passed into the subroutine when it is called.
Argument — the actual value or variable passed to a subroutine when it is called.
Return value — the single piece of data that a function sends back to the part of the program that called it.
Subroutine — a general term for both procedures and functions; a self-contained block of code that can be called from other parts of the program.
Local variable — a variable declared inside a subroutine that can only be accessed within that subroutine.
Scope — the region of a program where a variable can be accessed; local variables have scope limited to their subroutine.
Core concepts
Procedures vs Functions
Both procedures and functions are types of subroutines, but they have one key difference:
Procedures:
- Perform a task or action
- Do not return a value
- Called as standalone statements
- May modify data or produce output (e.g., display messages)
- Example uses: displaying menus, printing reports, updating databases
Functions:
- Perform a calculation or operation
- Always return exactly one value
- Called as part of an expression
- The returned value can be assigned to a variable or used in calculations
- Example uses: calculating totals, validating input, converting data formats
In CIE IGCSE pseudocode:
PROCEDURE DisplayWelcome()
OUTPUT "Welcome to the system"
ENDPROCEDURE
FUNCTION CalculateArea(Length, Width)
RETURN Length * Width
ENDFUNCTION
Defining and Calling Subroutines
Defining a subroutine means creating it with a name, parameters (if needed), and the code it will execute.
Calling a subroutine means executing it from another part of your program.
Procedure definition and call:
PROCEDURE PrintReceipt(CustomerName, TotalCost)
OUTPUT "Customer: ", CustomerName
OUTPUT "Total: $", TotalCost
ENDPROCEDURE
// Calling the procedure
PrintReceipt("Sarah Ahmed", 45.50)
Function definition and call:
FUNCTION ConvertToCelsius(Fahrenheit)
Celsius ← (Fahrenheit - 32) * 5 / 9
RETURN Celsius
ENDFUNCTION
// Calling the function
Temperature ← ConvertToCelsius(98.6)
OUTPUT Temperature
Key points about calling subroutines:
- The number of arguments must match the number of parameters
- The order of arguments must match the order of parameters
- Data types should be compatible
- Functions must be called where their return value can be used
Parameter Passing
Parameters allow you to pass data into subroutines, making them reusable with different values.
Types of parameter passing:
Pass by value (most common at IGCSE level):
- A copy of the argument's value is passed to the parameter
- Changes to the parameter inside the subroutine do not affect the original variable
- The original data remains unchanged
FUNCTION DoubleValue(Number)
Number ← Number * 2
RETURN Number
ENDFUNCTION
Value ← 10
Result ← DoubleValue(Value)
OUTPUT Value // Still outputs 10
OUTPUT Result // Outputs 20
Pass by reference (less common, but you should know it exists):
- The memory address of the variable is passed
- Changes to the parameter affect the original variable
- Used when you need a procedure to modify the original data
At IGCSE level, focus primarily on pass by value. The examiner typically expects you to understand that:
- Parameters receive copies of the data
- Multiple parameters are separated by commas
- Parameters act as local variables within the subroutine
Return Values and the RETURN Statement
Functions use the RETURN statement to send a value back to the calling code.
Important rules:
- A function must have at least one RETURN statement
- RETURN immediately exits the function
- Only one value can be returned
- The returned value's data type should match what the function promises
FUNCTION IsAdult(Age)
IF Age >= 18 THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
IF IsAdult(16) THEN
OUTPUT "Can vote"
ELSE
OUTPUT "Cannot vote"
ENDIF
You can return different data types:
- Numbers:
RETURN 42orRETURN 3.14 - Strings:
RETURN "Valid" - Booleans:
RETURN TRUEorRETURN FALSE - Calculated values:
RETURN X * Y + Z
Benefits of Using Procedures and Functions
Understanding why we use subroutines is testable knowledge:
Code reusability:
- Write code once, use it many times
- Reduces duplication
- Example: a validation function can check multiple inputs
Easier maintenance:
- Changes made in one place
- Fix bugs once rather than in multiple locations
- Updates are simpler to implement
Improved readability:
- Descriptive names explain what code does
- Main program becomes clearer
- Example:
CalculateVAT(Price)is clearer than repeated calculation code
Easier testing:
- Test subroutines independently
- Isolate problems more quickly
- Create test cases for specific functions
Team development:
- Different programmers can work on different subroutines
- Reduces conflicts in code
- Enables parallel development
Variable Scope and Lifetime
Local variables are declared inside a subroutine and can only be accessed within that subroutine.
FUNCTION CalculateDiscount(Price)
DiscountRate ← 0.1 // Local to this function
Saving ← Price * DiscountRate // Local to this function
RETURN Saving
ENDFUNCTION
OUTPUT Saving // This would cause an error - Saving doesn't exist here
Key points about scope:
- Parameters are local variables
- Local variables are created when the subroutine is called
- Local variables are destroyed when the subroutine ends
- Different subroutines can use the same variable names without conflict
- This prevents accidental data modification
Global variables (mentioned briefly at IGCSE level):
- Declared outside all subroutines
- Accessible throughout the program
- Generally avoided in good programming practice
Worked examples
Example 1: Writing a Function with Parameters
Question: Write a function called CalculateShippingCost that takes two parameters: Weight (in kg) and Distance (in km). The function should calculate shipping cost using the formula: Cost = Weight × 0.5 + Distance × 0.2. The function should return the total cost.
Solution (4 marks):
FUNCTION CalculateShippingCost(Weight, Distance)
Cost ← Weight * 0.5 + Distance * 0.2
RETURN Cost
ENDFUNCTION
Mark scheme:
- 1 mark: Correct function header with two parameters
- 1 mark: Correct calculation using both parameters
- 1 mark: RETURN statement
- 1 mark: Correct pseudocode syntax (FUNCTION/ENDFUNCTION)
Example 2: Identifying Procedure vs Function
Question: State whether each of the following should be written as a procedure or a function. Justify each answer.
a) A subroutine that displays a menu of options to the user. (2 marks) b) A subroutine that checks if a password contains at least 8 characters. (2 marks)
Solution:
a) Procedure. It performs an action (displaying output) but does not need to return a value. (2 marks: 1 for procedure, 1 for justification)
b) Function. It needs to return a Boolean value (TRUE/FALSE) indicating whether the password is valid, so the program can make decisions based on this result. (2 marks: 1 for function, 1 for justification)
Example 3: Completing a Function Call
Question: The following function converts pounds (£) to euros (€):
FUNCTION ConvertToEuros(Pounds)
ExchangeRate ← 1.17
Euros ← Pounds * ExchangeRate
RETURN Euros
ENDFUNCTION
Write a statement that calls this function to convert £250 and stores the result in a variable called HolidayMoney. (2 marks)
Solution:
HolidayMoney ← ConvertToEuros(250)
Mark scheme:
- 1 mark: Correct function call with argument 250
- 1 mark: Assignment to HolidayMoney variable
Common mistakes and how to avoid them
Confusing arguments and parameters. Parameters are in the subroutine definition; arguments are the values you pass when calling. Remember: parameters are placeholders, arguments are actual values.
Forgetting the RETURN statement in functions. Every function must return a value. If your function performs a calculation, always include
RETURNwith the result. Procedures do not use RETURN.Using the return value of a procedure. Procedures don't return values, so you cannot write
X ← MyProcedure(). Call procedures as standalone statements:MyProcedure().Mismatching the number or order of arguments. If a function has parameters
(Length, Width), you must call it with two arguments in that order. WritingCalculateArea(10)with only one argument will cause an error.Trying to access local variables outside their subroutine. Variables declared inside a function only exist within that function. Plan which variables need to be parameters and which can be local.
Not using descriptive names. Names like
Func1(X, Y)tell you nothing. Use names likeCalculateArea(Length, Width)that clearly describe purpose and expected inputs.
Exam technique for "Programming concepts: procedures and functions, including parameter passing and return values"
Command word "Write" means produce code. Use correct pseudocode syntax with PROCEDURE/ENDPROCEDURE or FUNCTION/ENDFUNCTION. Include appropriate parameter names and, for functions, a RETURN statement. Usually worth 3-5 marks.
Command word "Explain" requires you to give reasons. For example, "Explain one benefit of using functions" needs you to state the benefit AND explain how it helps (e.g., "Code reusability — the function can be called multiple times with different arguments without rewriting the code").
Show the complete subroutine structure. Even if the question focuses on one part, write the full header and closing statement. Examiners deduct marks for incomplete syntax.
When comparing procedures and functions, always mention the return value as the key difference. State that functions return a value; procedures do not. This distinction appears frequently in exam questions worth 2-3 marks.
Quick revision summary
Procedures and functions are subroutines that structure programs into manageable blocks. Procedures perform tasks without returning values; functions calculate and return a single value. Parameters pass data into subroutines using arguments when called. Local variables exist only within their subroutine scope. Benefits include code reusability, easier maintenance, improved readability, and simpler testing. Always match argument count and order to parameters, use RETURN in functions, and apply descriptive naming conventions for clarity.