Skip to topic
    ← Back to course topics

    Subprograms — Edexcel GCSE Computer Science

    Verified courseTeaching note checked against the official specification.

    Subprograms explained

    Subprograms covers 3 specification points, including Be able to write programs that use… and Be able to write functions that may or may not….

    Read the full explanation

    Open a point below for its explanation and revision guidance.

    6.6.1 be able to write programs that use pre-existing (built-in, library) and user-devised subprograms (procedures, functions)

    This criterion requires you to write programs that call and create subprograms. A subprogram is a named block of code that performs a task and can be reused. Pre-existing subprograms are already available: built-in ones such as print() or len() come with the language, while library ones are imported, for example math.sqrt(). User-devised subprograms are written by you. A procedure performs an action and may not return a value, while a function returns a value to the point of call. You should define each subprogram with a clear name and parameters, call it with matching arguments, and use any returned value. For example, define a function to convert marks to a grade and call it for each pupil, or write a procedure that prints a formatted receipt. Assessment checks that your code defines and calls subprograms correctly and uses the right kind for the task.

    6.6.2 be able to write functions that may or may not take parameters but must return values, and procedures that may or may not take parameters but do not return values

    A subprogram is a named block of code you can call repeatedly. A function must return a value to the caller, so the call can be used inside an expression, for example total = add(3, 4). A procedure does not return a value; it is called as a statement, for example printMenu(). Either kind may take parameters or none: a function with no parameters might return a random number, and a procedure with parameters might display a formatted name. Decide the kind by asking whether a value must come back. If yes, write a function with a return statement that supplies a value of the expected type on every path. If no, write a procedure and pass any data it needs as parameters. Parameters are the named inputs in the definition; arguments are the actual values supplied at the call.

    6.6.3 understand the difference between and be able to write programs that make appropriate use of global and local variables

    A global variable is declared in the main program and can be read or changed by any subprogram. A local variable is created inside a subprogram and exists only while that subprogram runs, so code outside cannot see it. Local variables keep subprograms self-contained: two subprograms can each use a variable named count without clashing. Globals are convenient for values many parts of the program share, such as a score, but overusing them makes code hard to trace because any subprogram might change them. A good approach is to pass needed data in as parameters, keep working values local, and return results, using globals only for genuinely shared state.

    Your focus

    1. Call a built-in or imported library subprogram correctly in a program.
    2. Define and call a user-devised procedure or function with suitable parameters.
    3. Distinguish a procedure from a function and use a returned value where required.
    Show all 9 objectives
    1. Identify whether a given subprogram should be written as a function or a procedure from how its result is used.
    2. Write a function that returns a value, with or without parameters.
    3. Write a procedure that performs a task without returning a value, with or without parameters.
    4. Explain the difference in scope and lifetime between global and local variables.
    5. Trace a program and state the value of a global or local variable at a given point.
    6. Write a program that uses local variables and parameters appropriately, with globals only where shared state is needed.

    Subprograms exam tips

    Marking Points
    • A built-in subprogram is supplied by the language and called directly, such as print() or len().
    • A library subprogram must be imported before use, for example importing a maths module and then calling its square-root function.
    • A user-devised subprogram is defined by the programmer with a name, and may take parameters.
    • A procedure performs an action, while a function returns a value that the calling code can use.
    • Calls must pass arguments that match the parameters in number and meaning, and any returned value must be used or stored.
    • A function must return a value, so its call can be used where a value is required, such as in an assignment or expression.
    • A procedure does not return a value and is called as a standalone statement.
    • Both functions and procedures may take zero or more parameters.
    • Parameters are declared in the subprogram definition; arguments are the values passed when it is called.
    • A function needs a return statement that provides a value on every possible path.
    • Choosing function or procedure depends on whether the caller needs a value back.
    • A global variable is declared outside subprograms and is accessible throughout the program.
    • A local variable is declared inside a subprogram and is accessible only within that subprogram.
    • Local variables are created when the subprogram is called and cease to exist when it ends.
    • Two subprograms may each have a local variable with the same name without conflict.
    • Changing a global inside a subprogram affects the value seen elsewhere in the program.
    • Appropriate use means passing data as parameters and returning results where possible, reserving globals for values genuinely shared across the program.
    Examiner Tips
    • 💡Decide whether the task needs a returned value before choosing a function or a procedure.
    • 💡Name each user-devised subprogram clearly so its purpose is obvious at the call site.
    • 💡Check that every call supplies arguments matching the parameters and that any returned value is used.
    • 💡For each subprogram in a question, state whether a value must be returned and justify the choice from how the call is used.
    • 💡Check that every path through a function body reaches a return statement with a suitable value.
    • 💡When tracing code, follow the arguments into the parameters and the returned value back to the call site.
    • 💡State the scope of each variable in a code snippet and explain where it can be accessed.
    • 💡When tracing, track global values across calls and treat each local variable as fresh on every call.
    • 💡Justify a design choice by naming the scope that keeps the data available only where it is needed.
    Common Mistakes
    • Calling a library subprogram without importing its module; correct this by adding the import before the call.
    • Confusing procedures and functions; correct this by checking whether the subprogram must return a value and defining it accordingly.
    • Passing the wrong number or type of arguments; correct this by matching each argument to the declared parameter.
    • Thinking every subprogram that has a name and brackets is a function: a procedure is identified by not returning a value, not by its syntax alone.
    • Believing a function cannot take parameters: functions may take none, one or several parameters and still must return a value.
    • Assuming a procedure can never receive data: procedures may take parameters even though they return nothing.
    • Writing a function that prints a result but never returns it: printing is output, not returning a value to the caller.
    • Assuming a local variable can be read in the main program after the subprogram finishes: it no longer exists outside its subprogram.
    • Believing a local variable in one subprogram is visible in another: each subprogram has its own scope.
    • Thinking a global variable must be used whenever two subprograms need the same data: passing parameters and returning values is often clearer.
    • Confusing a parameter with a global: a parameter is local to the subprogram that receives it.