Skip to main content
EnglandComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Functions and procedures
  3. Parameters
  4. Built-in and user-devised subprograms
  5. Operators in programs
  6. Global and local variables
  7. Try this

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

Sources & how we know this