How do you write functions and procedures with parameters, and what is the difference between global and local variables?
Write programs that use pre-existing and user-devised subprograms (procedures, functions), write functions that return values and procedures that do not, with or without parameters, use the arithmetic, relational and logical operators, and use global and local variables appropriately.
A focused answer to Edexcel GCSE Computer Science 6.5 and 6.6, covering functions and procedures, parameters and return values, built-in and user-defined subprograms, the operators, and global versus local variables in Python.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
Edexcel wants you to write and use subprograms: functions (which return a value) and procedures (which do not), with or without parameters; to use pre-existing (built-in) and user-devised subprograms; to use the arithmetic, relational and logical operators in programs; and to use global and local variables appropriately.
Functions and procedures
# A function: returns a value
def area_of_rectangle(width, height):
return width * height
# A procedure: does a task, returns nothing
def greet(name):
print("Hello " + name)
print(area_of_rectangle(6, 4)) # 24 (uses the returned value)
greet("Sam") # Hello Sam (no value returned)
The exam discriminator is returns a value (function) versus does not (procedure). A function's result is used in an expression (such as print(area_of_rectangle(6, 4))), whereas a procedure is called for its effect (such as printing). Edexcel states that functions may or may not take parameters but must return values, and procedures may or may not take parameters but do not return values.
Parameters
In def area_of_rectangle(width, height), width and height are parameters; in the call area_of_rectangle(6, 4), the arguments 6 and 4 are passed in. Parameters are what make a subprogram general: the same area_of_rectangle works for any width and height. A subprogram may have no parameters, one, or several.
Built-in and user-devised subprograms
You use both kinds constantly. Built-in subprograms like len() and round() do common jobs reliably, so you do not reinvent them. Your own subprograms (defined with def) capture the tasks specific to your problem, applying decomposition so each does one job. Together they keep programs short and organised.
Operators in programs
These are the same operators as in Topic 1, now in Python form: // is integer division (17 // 5 is 3), % is modulus (17 % 5 is 2), and ** is exponentiation (2 ** 4 is 16). Relational operators give Boolean results for conditions, and and, or, not combine them, for example if age >= 13 and age <= 19:.
Global and local variables
TAX = 0.20 # global constant, usable anywhere
def add_tax(price):
extra = price * TAX # 'extra' is local to add_tax
return price + extra
print(add_tax(100)) # 120.0
# print(extra) # error: 'extra' does not exist here
The advantage of local variables is that they keep a subprogram independent: its variables cannot be accidentally changed by, or clash with, the rest of the program, which makes the code easier to debug and reuse. Globals are useful for values needed throughout the program (often constants), but overusing them makes programs harder to follow, so prefer locals and parameters for passing data in and return for passing results out.
Try this
Q1. State the difference between a function and a procedure. [1 mark]
- Cue. A function returns a value; a procedure performs a task but does not return a value.
Q2. State where a local variable can be used. [1 mark]
- Cue. Only inside the subprogram in which it is declared.
Exam-style practice questions
Practice questions written in the style of Pearson Edexcel exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Edexcel 20225 marksWrite a Python function called area_of_rectangle that takes a width and a height as parameters and returns the area. Then write a line that calls it for a width of 6 and a height of 4 and prints the result.Show worked answer →
Define a function with two parameters that returns the product, then call it.
def area_of_rectangle(width, height):
return width * height
print(area_of_rectangle(6, 4))
This prints 24. Markers reward defining the function with two parameters, returning width times height (not printing inside the function, since a returned value is required), calling it with the arguments 6 and 4, and printing the returned result. A function must return, not just print.
Edexcel 20214 marksExplain the difference between a local variable and a global variable, and state one advantage of using local variables.Show worked answer →
A local variable is declared inside a subprogram and can only be used within that subprogram; it exists only while the subprogram runs. A global variable is declared in the main program and can be used anywhere in the program.
One advantage of local variables: they keep a subprogram self-contained, so its variables do not interfere with, or get accidentally changed by, other parts of the program, which makes the code easier to debug and reuse.
Markers reward the difference (local: only inside its subprogram; global: usable anywhere) and a valid advantage of local variables (avoids clashes or accidental changes, keeps subprograms independent and reusable).
Related dot points
- Identify the structural components of programs (constants, variables, initialisation, assignment, sequence, selection, repetition, iteration, data structures, subprograms, parameters, input/output) and write programs that use sequencing, selection, repetition (count-controlled, condition-controlled) and iteration with single entry and exit points.
A focused answer to Edexcel GCSE Computer Science 6.2, covering the structural components of programs and writing Python programs that use sequence, selection, count-controlled and condition-controlled repetition, and iteration.
- Write programs that make appropriate use of primitive data types (integer, real, Boolean, char) and one- and two-dimensional structured data types (string, array, record), and that make appropriate use of variables and constants.
A focused answer to Edexcel GCSE Computer Science 6.3.1 and 6.3.2, covering the primitive data types (integer, real, Boolean, char), structured types (string, array, record) in one and two dimensions, and using variables and constants.
- Use decomposition and abstraction to solve problems; read, write, analyse and refine programs; convert algorithms (flowcharts, pseudocode) into programs; use techniques (layout, indentation, comments, meaningful identifiers, white space) for readable code; identify, locate and correct logic, syntax and runtime errors; and evaluate a program's fitness for purpose and efficiency.
A focused answer to Edexcel GCSE Computer Science 6.1, covering using decomposition and abstraction in programs, converting algorithms to code, writing readable code, identifying and correcting errors, and evaluating fitness for purpose.
- Follow and write algorithms that use arithmetic operators (addition, subtraction, division, multiplication, modulus, integer division, exponentiation), relational operators (equal to, less than, greater than, not equal to, less than or equal to, greater than or equal to) and logical operators (AND, OR, NOT).
A focused answer to Edexcel GCSE Computer Science 1.2.3, covering the arithmetic operators including modulus and integer division, the relational operators, and the logical operators AND, OR and NOT in algorithms.
- Understand the need for and write programs that implement validation (length check, presence check, range check, pattern check) and authentication (ID and password, lookup).
A focused answer to Edexcel GCSE Computer Science 6.4.3 and 6.4.4, covering the need for validation, the length, presence, range and pattern checks, and authentication by ID and password lookup in Python.
Sources & how we know this
- Pearson Edexcel GCSE (9-1) Computer Science (1CP2) specification — Pearson (2020)
- Pearson Edexcel GCSE Computer Science Programming Language Subset (Python) — Pearson (2020)