Using subprograms (Edexcel GCSE Computer Science): Revision Notes
Using subprograms
What is a subprogram?
A subprogram is like a mini-program within your main programme - it's a self-contained block of code that performs one specific task. Think of it like a recipe for a particular dish that you can use whenever you need it, rather than writing out all the steps every time.
The brilliant thing about subprograms is that you only need to write them once, but you can use them as many times as you want. This works both within your current programme and even in completely different programmes if you save them in a library.
Definition: A subprogram is a self-contained block of code that performs a specific task within a larger programme. Subprograms only need to be written once and can be used as many times as needed within a programme or saved in a library and used in other programmes.
Types of subprograms
Pre-existing subprograms
Programming languages come with lots of ready-made subprograms that handle common tasks. These save you tonnes of time because someone else has already done the hard work!
Built-in functions are already part of the programming language. For example:
- print() - displays output on screen
- len() - counts characters in a string
Library functions need to be imported before you can use them:
- random.randint() - generates random numbers
- maths.floor() - rounds numbers down
Pre-existing subprograms are incredibly valuable because they represent tested, optimised code that has been used by thousands of programmers. Using them reduces the chance of bugs and saves significant development time.
User-defined subprograms
Sometimes you need something specific that doesn't exist yet, so you can create your own custom subprograms. These are tailored exactly to what your programme needs to do.
Key concepts in subprograms
Abstraction
Abstraction means you can use a subprogram without needing to understand exactly how it works inside. It's like using a calculator - you press the buttons and get an answer without knowing the complex electronics inside.
This lets programmers focus on the big picture of their programme rather than getting lost in every tiny detail. Abstraction is essential for managing complexity in large software projects.
Generalisation
Generalisation makes subprograms even more powerful through parameter passing. This means you can send different values into a subprogram so it can work on different data each time.
Worked Example: Generalisation with Parameters
Instead of creating multiple specific subprograms:
calculateArea5x3()- calculates area of 5×3 rectanglecalculateArea10x7()- calculates area of 10×7 rectangle
You can create one general subprogram:
calculateArea(length, width)- calculates area of any rectangle
This single subprogram can handle any rectangle size when you pass in the length and width values as parameters.
Why use subprograms?
Using subprograms brings several major benefits that make programming more efficient and manageable:
-
Break down complexity - Large, complicated programmes become easier to handle when split into smaller, manageable chunks
-
Make logic clearer - Each subprogram has a meaningful name that describes what it does, making your code much easier to read and understand
-
Simplify maintenance - When you need to fix a bug or make improvements, you only need to change the code in one place, not throughout your entire programme
-
Enable code reuse - Write once, use many times! This saves loads of development time and reduces mistakes
-
Store common tasks - Useful subprograms can be saved in libraries and shared between different programmes and programmers
-
Support teamwork - Different team members can work on different subprograms at the same time, speeding up development
Memory Aid - "BERT" for remembering subprogram benefits:
- Break down complexity
- Enable code reuse
- Reduce maintenance effort
- Team collaboration support
Exam tips and worked examples
When explaining benefits of subprograms in exams, remember to give reasons rather than just listing features. Look for command words like "explain" - this means you need to include valid points supported by explanations.
Worked Example: Exam Question
Question: Explain two ways subprograms speed up programme development (4 marks)
Answer:
-
Programmers don't have to write and debug all the code themselves because there are pre-existing built-in and library subprograms they can use to perform common tasks.
-
A team of programmers rather than an individual can work on the programme because different subprograms can be allocated to different programmers, allowing parallel development.
Key Exam Strategy: Use connecting words like "because" or "so that" to show you're providing explanations, not just descriptions. This demonstrates understanding rather than memorisation.
Key Points to Remember:
- Subprograms are self-contained blocks of code that perform specific tasks and can be reused multiple times
- Pre-existing subprograms (built-in and library functions) save development time by providing ready-made solutions
- User-defined subprograms let you create custom functions for specific needs
- Abstraction allows you to use subprograms without understanding their internal workings
- Parameter passing enables generalisation - making subprograms work with different input values
- Major benefits include breaking down complexity, improving code clarity, easier maintenance, enabling reuse, and supporting teamwork