Using Subroutines (AQA GCSE Computer Science)
Revision Note
Using Subroutines
Why do we use subroutines?
Subroutines are used to be able to decompose a big problem into smaller parts to make it easier to solve
Subroutines are split into 2 types of subprogram
Functions
Procedures
Functions and procedures are a type of sub-program, a sequence of instructions that perform a specific task or set of tasks
Subprograms are often used to simplify a program by breaking it into smaller, more manageable parts
The advantages of using subprograms are:
Avoid duplicating code and can be reused throughout a program
Improve the readability and maintainability of code
Perform calculations, to retrieve data, or to make decisions based on input
Parameters are values that are passed into a sub program
Parameters can be variables or values and they are located in brackets after the name of the sub program
Example:
function taxCalculator(pay,taxcode)
ORdef taxCalculator(pay,taxcode)
Sub programs can have multiple parameters
To use a sub program you 'call' it from the main program
Functions and Procedures
What's the difference between a function and procedure?
A Function returns a value whereas a procedure does not
Concept | Pseudocode | Python |
---|---|---|
Creating a function |
|
|
Calling a function |
OR
|
OR
|
Creating a procedure |
|
|
Calling a procedure |
|
|
Examples
A Python program using a function to calculate area and return the result
Two options for main program are shown, one which outputs the result (# 1) and one which stores the result so that it can be used at a later time (# 2)
Functions |
---|
|
A Python program using procedures to display a menu and navigate between them
Procedures are defined at the start of the program and the main program calls the first procedure to start
In this example, no parameters are needed
Procedures |
---|
|
What is a local variable?
A local variable is a variable declared within a specific scope, such as a function or a code block
Local variables are accessible only within the block in which they are defined, and their lifetime is limited to that particular block
Once the execution of the block ends, the local variable is destroyed, and its memory is released
Python example
In this python code, you can see that the localVariable
(with the value 10) is declared inside of the function printValue
. This means that only this function can access and change the value in the local variable. It cannot be accessed by other modules in the program.
Local variables |
---|
|
Worked Example
An economy-class airline ticket costs £199. A first-class airline ticket costs £595.
(A) Create a function, flightCost(),
that takes the number of passengers and the type of ticket as parameters, calculates and returns the price to pay.
You do not have to validate these parameters
You must use either:
Pseudocode, or
a high-level programming language that you have studied [4]
(B) Write program code, that uses flightCost()
, to output the price of 3 passengers flying economy.
You must use either:
Pseudocode, or
a high-level programming language that you have studied [3]
How do I answer this question?
(A)
Define the function, what parameters are needed? where do they go?
How do you calculate the price?
Return the result
(B)
How do you call a function?
What parameters does the function need to return the result?
Answers
Part | Pseudocode | Python |
---|---|---|
A |
|
|
B |
OR
|
OR
|
Last updated:
You've read 0 of your 10 free revision notes
Unlock more, it's free!
Did this page help you?