1CP2 P1Edexcel GCSE Computing · 1CP2 Paper 1 (Programming)
Subprograms — Functions and Procedures
Functions (return values) vs procedures (no return). Parameters and arguments. Scope. Why use subprograms.
—
Best score
—
Last attempt
Best attempt progress
0
XP earned
Watch first
Craig n DaveSubroutines in pseudocode
Reference & Examples
Why use subprograms?
Avoid repeating code — write once, call many times (reusability).
Break complex problems into smaller parts (decomposition).
Easier to test each part separately.
Easier to read and maintain — meaningful names explain what code does.
Teams can work on different subprograms simultaneously.
Procedures vs functions
Procedure: performs a task. Does NOT return a value. Called by name.
Function: performs a task AND returns a value. Used in expressions.
Procedure example: CALL printGreeting("Ander")
Function example: result = square(5) — result gets the returned value.
In Edexcel pseudocode: PROCEDURE ... END PROCEDURE. FUNCTION ... RETURN ... END FUNCTION.
Parameters and arguments
Parameter: the variable name in the subprogram definition. Placeholder.
Argument: the actual value passed when calling the subprogram.
FUNCTION add(a, b) — a and b are parameters.
result = add(3, 7) — 3 and 7 are arguments.
Parameters allow subprograms to work with different data each time they are called.
Variable scope
Local variable: declared inside a subprogram. Only visible within it. Lost when subprogram ends.
Global variable: declared in the main program. Visible everywhere.
Best practice: use local variables where possible. Avoid using globals inside subprograms.
Scope prevents naming conflicts between different parts of a program.
If a local variable has the same name as a global, the local one is used inside the subprogram.
Procedure — no return value
PROCEDURE greet(name)
PRINT "Hello, " + name + "!"
PRINT "Welcome to the program."
END PROCEDURE
// Calling the procedure:
CALL greet("Ander")
// Output:
// Hello, Ander!
// Welcome to the program.
Function — with return value
FUNCTION calculateArea(length, width)
area = length * width
RETURN area
END FUNCTION
// Calling the function:
roomArea = calculateArea(5, 3)
PRINT roomArea // Output: 15
// Can use directly in expression:
PRINT "Area = " + STR(calculateArea(4, 7))
Function with local variable
FUNCTION isPrime(n)
// n is a parameter — local to this function
IF n < 2 THEN
RETURN False
END IF
FOR i = 2 TO n - 1
IF n MOD i == 0 THEN
RETURN False
END IF
END FOR
RETURN True
END FUNCTION
// The variable i is local — does not exist outside isPrime
Exam practice — 5 questions · 12 marks · 1CP2 Paper 1 style
⚡ Write your pseudocode or answer — the AI marks against the Edexcel 1CP2 Paper 1 mark scheme.
1 markProcedure vs function1CP2 Paper 1
What is the key difference between a function and a procedure?
A Functions are faster than procedures
B A function returns a value; a procedure does not
C Procedures can have parameters but functions cannot
D Functions are called using CALL; procedures are not
4 marksWrite a function1CP2 Paper 1
Write a function called maxValue that takes two parameters, a and b, and returns the larger of the two values. Show how you would call it to find the larger of 15 and 23. (4 marks)
Hint: FUNCTION keyword, parameters in brackets, IF to compare, RETURN each result. Call it and store/print the result.
+40 XP
1 markLocal scope1CP2 Paper 1
A variable called total is declared inside a function. What happens to it when the function finishes?
A It becomes a global variable
B It is destroyed — it no longer exists outside the function
C It keeps its value and can be used anywhere in the program
D It is automatically returned from the function
4 marksIdentify and fix the problem1CP2 Paper 1
Study this pseudocode:
FUNCTION double(n)
result = n * 2
END FUNCTION
total = double(5)
PRINT total
Explain what is wrong and write the corrected version. (3 marks)
Hint: The function calculates but does not RETURN. Without RETURN, the calling code gets no value back.
+40 XP
1 markParameter definition1CP2 Paper 1
In the definition: FUNCTION power(base, exponent), what are "base" and "exponent"?