Passing Parameters (AQA GCSE Computer Science): Revision Notes
Passing parameters
What are subroutines?
A subroutine is a separate block of code that you write outside your main programme. Think of it like a mini-program that has its own name (called an identifier) and can be called (run it) as many times as you want throughout your programme.
The key advantage of subroutines is code reusability - instead of writing the same code multiple times, you create it once and call it whenever needed.
This is really useful because instead of writing the same code over and over again, you can just call your subroutine whenever you need that particular task done.
Basic subroutine structure
Here's how you create a simple subroutine that doesn't use parameters:
Basic Subroutine Structure Example
SUBROUTINE timestableuser()
OUTPUT 'enter times table'
num ← USERINPUT
FOR x ← 1 TO 10
OUTPUT num * x
ENDFOR
ENDSUBROUTINE
To use this subroutine, you would call it like this:
timestableuser()
Notice the empty brackets () after the subroutine name - this is where parameters go when we use them.
What are parameters?
Parameters are pieces of information that you can send to a subroutine to make it more flexible and useful. Think of them like ingredients you give to a recipe - the subroutine (recipe) uses these ingredients (parameters) to do its job.
Parameter Analogy
Just like a recipe can make different dishes depending on the ingredients you provide, a subroutine can perform different tasks depending on the parameters you pass to it.
Without parameters, a subroutine always does exactly the same thing. With parameters, you can customise what the subroutine does each time you call it.
Using parameters in subroutines
Let's look at how we can improve our times table subroutine by adding parameters:
Improved Subroutine with Parameters
SUBROUTINE timestable(tt, nums)
FOR x ← 1 TO nums
OUTPUT tt * x
ENDFOR
ENDSUBROUTINE
In this example:
- tt is a parameter that represents which times table we want
- nums is a parameter that represents how many numbers we want to calculate
Calling subroutines with parameters
When you call a subroutine that uses parameters, you need to provide the actual values:
Calling Subroutines with Different Parameters
timestable(8, 10)
timestable(9, 12)
What happens:
First call: timestable(8, 10)
- The values
8and10are passed to the subroutine - Inside the subroutine, tt becomes 8 and nums becomes 10
- This prints the 8 times table from to
Second call: timestable(9, 12)
- The values
9and12are passed to the subroutine - This time tt becomes 9 and nums becomes 12
- This prints the 9 times table from to
Benefits of using parameters
Using parameters makes your subroutines much more powerful and efficient:
-
Flexibility: One subroutine can handle many different situations instead of writing separate subroutines for each case
-
Reusability: You can use the same subroutine throughout your programme with different values
-
Less code: Instead of writing new code for each times table, you write one subroutine that works for any times table
-
Easier maintenance: If you need to change how the times table is displayed, you only need to update one subroutine
Exam tips
Key Rules to Remember:
- Always remember to include the brackets
()when defining and calling subroutines, even if they're empty - When calling a subroutine with parameters, make sure you pass the values in the correct order
- Parameters act like variables inside the subroutine - they can be used in calculations and outputs
- The parameter names in the subroutine definition don't have to match the variable names you pass in
Remember!
Key Points to Remember:
- Parameters are values you pass into subroutines to customise their behaviour
- Subroutines with parameters are more flexible than those without
- Parameter order matters - the first value you pass goes to the first parameter, and so on
- Parameters make code reusable - one subroutine can handle many different situations
- Always use brackets when defining and calling subroutines, whether they have parameters or not