Skip to topic
    ← Back to course topics

    Subprograms — Edexcel GCSE Computer Science

    Test yourself on Subprograms with PEARSON EDEXCEL GCSE practice questions.

    Start free

    7 days Premium · Then free forever · No card, no charge

    Subprograms explained

    This topic covers the use of subprograms, including both procedures and functions, to structure code effectively.

    Read the full explanation

    Students must understand how to implement these, manage parameters, handle return values, and distinguish between local and global variable scopes.

    Read the Subprograms study guideFull revision notes for Edexcel GCSE Computer Science

    What to demonstrate

    1. Correct use of built-in and user-devised subprograms
    2. Correct implementation of functions that return values
    3. Correct implementation of procedures that do not return values
    Show all 6 objectives
    1. Appropriate use of parameters within subprograms
    2. Correct application of local versus global variable scope
    3. Single entry and exit points from code blocks and subprograms

    Subprograms exam tips

    Topic Overview

    Subprograms, also known as procedures or functions, are reusable blocks of code that perform specific tasks. In Edexcel GCSE Computer Science, you will learn how to define and call subprograms to avoid repetition, improve code readability, and simplify debugging. Subprograms can take parameters (inputs) and return values, making them powerful tools for modular programming.

    Understanding subprograms is essential because they form the backbone of structured programming. They allow you to break down complex problems into smaller, manageable parts. This topic also introduces key concepts like local and global variables, parameter passing (by value vs. by reference), and the scope of variables. Mastery of subprograms is crucial for writing efficient, maintainable code and is frequently tested in both written exams and programming tasks.

    In the wider subject, subprograms connect to topics such as algorithms, data structures, and object-oriented programming. They are used in nearly every programming language and are fundamental to software development. By learning subprograms, you build a foundation for more advanced concepts like recursion and event-driven programming.

    Key Concepts
    • →Definition and call: A subprogram is defined using a name, parameters (optional), and a body. It is executed when called by name, possibly with arguments.
    • →Parameters and arguments: Parameters are variables in the subprogram definition; arguments are the actual values passed when calling. Understand the difference between formal and actual parameters.
    • →Return values: Functions return a value using the 'return' statement; procedures do not return a value (they perform an action). In Python, a function returns None if no return is specified.
    • →Local vs. global variables: Variables declared inside a subprogram are local (only accessible within it). Global variables are declared outside and can be accessed anywhere, but modifying them inside a subprogram requires the 'global' keyword (in Python).
    • →Parameter passing: By default, Python passes parameters by reference for mutable objects (e.g., lists) and by value for immutable objects (e.g., integers). However, the exam focuses on the effect: changes to mutable objects inside a subprogram affect the original.
    Marking Points
    • Correct use of built-in and user-devised subprograms
    • Correct implementation of functions that return values
    • Correct implementation of procedures that do not return values
    • Appropriate use of parameters within subprograms
    • Correct application of local versus global variable scope
    • Single entry and exit points from code blocks and subprograms
    Examiner Tips
    • 💡Always check if a subprogram needs to return a value to determine if it should be a function or a procedure
    • 💡Use meaningful identifiers for subprogram names to improve code readability
    • 💡Ensure parameters are clearly defined and used consistently within the subprogram logic
    • 💡Practice tracing code that uses local variables to ensure you understand scope limitations
    • 💡Always check the scope of variables: exam questions often ask you to trace the output of code with subprograms. Identify which variables are local and global to avoid mistakes.
    • 💡When writing subprograms, use meaningful names and include comments to explain the purpose and parameters. This demonstrates good programming practice and can earn you marks for style.
    • 💡For parameter passing questions, remember that in Python, integers and strings are immutable. If a subprogram modifies a parameter that is an integer, the original variable outside remains unchanged. Draw a memory diagram if needed.
    Common Mistakes
    • Confusing the difference between a function (returns a value) and a procedure (does not return a value)
    • Incorrectly accessing global variables inside a subprogram instead of passing them as parameters
    • Failing to return a value in a function
    • Over-reliance on global variables leading to poor code structure
    • Misconception: A subprogram can change a variable passed as an argument, regardless of type. Correction: In Python, immutable types (int, float, string, tuple) cannot be changed inside a subprogram; any assignment creates a new local variable. Mutable types (list, dict) can be modified in place.
    • Misconception: Using the same variable name inside and outside a subprogram means they are the same variable. Correction: If a variable is assigned inside a subprogram, it becomes local (unless declared global). The global variable with the same name is unaffected.
    • Misconception: A function must always return a value. Correction: In Python, a function without a return statement returns None. It is still a valid subprogram, but it is a procedure, not a function in the strict sense.
    Frequently Asked Questions
    What is the difference between a procedure and a function in GCSE Computer Science?
    A procedure is a subprogram that performs a task but does not return a value. A function is a subprogram that returns a single value. In Python, all subprograms are defined using 'def', but a function uses 'return' to send back a result, while a procedure typically does not. However, in Python, a procedure still returns None implicitly.
    How do you pass a list to a subprogram and modify it?
    In Python, lists are mutable and passed by reference. This means if you pass a list to a subprogram and modify its elements (e.g., append, change an index), the original list outside the subprogram is also changed. However, if you assign a new list to the parameter variable inside the subprogram, it does not affect the original list.
    What is a local variable and why is it important?
    A local variable is declared inside a subprogram and can only be used within that subprogram. It is important because it prevents accidental interference with other parts of the program. Local variables are created when the subprogram is called and destroyed when it ends, which saves memory and reduces bugs.
    Can a subprogram call itself?
    Yes, this is called recursion. A recursive subprogram calls itself with a different argument, usually to solve a smaller version of the same problem. Recursion must have a base case to stop the calls, otherwise it will cause infinite recursion and a stack overflow error. Recursion is a more advanced topic but is sometimes covered in GCSE.
    How do I decide whether to use a subprogram or just write the code inline?
    Use a subprogram when you need to perform the same task multiple times, or when you want to break a complex problem into smaller, manageable parts. Subprograms make code reusable, easier to read, and simpler to test. If the code is only used once and is very short, inline code might be acceptable, but subprograms are generally preferred for good programming practice.
    What does 'return' do in a subprogram?
    The 'return' statement exits the subprogram and sends a value back to the caller. For example, 'return x + y' will compute the sum and give it to the line that called the function. If there is no 'return', the subprogram returns None. You can also use 'return' without a value to exit early.