Professional Documents
Culture Documents
1.2.11 Procedures and Functions
1.2.11 Procedures and Functions
1.2.11 Procedures and Functions
Subroutines
A subroutine can be thought of as a mini program. It sits in the background waiting to be called. When it is
called, the main program pauses, whist the subroutine is executed. Once the subroutine has finished,
attention turns back to the main program.
Procedures
A procedure is a type of subroutine which, when called, executes its code, but doesn’t pass any data back
into the main program.
When programs with procedures are run, the procedures are initially ignored and the first few lines of the
main program are executed. When a procedure call is found in the main program, the main program pauses
and the procedure is executed. After the lines of code in the procedure are executed, the main program is
resumed.
In the example below, this particular subroutine is known as a procedure because it doesn’t pass any data
back into the main program.
Variable Scope
and Subroutines
In programing there are
two main classifications
of variables:
Computer Science UK Membership Site Licence: Do not share outside your centre. 1
A-Level Computer Science
If a subroutine doesn’t assign its own variable, it will use the global variable:
Here a global variable is being assigned (as the assignment is
being done in the main program)
Then, as the subroutine doesn’t assign its own
variable, it uses the global
variable ‘s’.
Computer Science UK Membership Site Licence: Do not share outside your centre. 2
A-Level Computer Science
Using our current knowledge, if we wanted this program to allow the user to select a times table of their
choice, we would have to create a subroutine for each times table and then ask the user to select their choice.
Here, you can see that the use
of procedures written in this
way has a drawback. Although
it is great that our code is self-
contained, the issue is that the
code is being duplicated (with
only minor changes).
Programmers like efficiency,
and duplicating code is not
efficient!
Computer Science UK Membership Site Licence: Do not share outside your centre. 3
A-Level Computer Science
In order to be more efficient (less code) what we really want is to have one procedure to do the
multiplication and PASS the
user’s times table choice into the
procedure.
Functions
The previous example showed how data can be passed into a subroutine. A function is a subroutine that can
also return data back into the main program. This can be highly useful in programming.
The way to do this is to assign a variable to the function call so that the variable is assigned any value that
the function returns.
variable_name = function_name(parameter)
Computer Science UK Membership Site Licence: Do not share outside your centre. 4
A-Level Computer Science
Here the arrows show how data (an argument) is being passed from the main program into the function’s
parameter ‘x’. ‘num1’ is also known as a parameter and the value stored in ‘num1’ is known as an argument.
When the function is finished executing its commands it returns the value back into the main program. The
value is returned into the variable that the function call is assigned to (in this case it is answer).
Computer Science UK Membership Site Licence: Do not share outside your centre. 5
A-Level Computer Science
Questions
1. What is a subroutine and what types of subroutine are there?[2]
2. What is the difference between a ‘Local’ and ‘Global’ variable? [2]
3. What is the difference between a ‘Procedure’ and a ‘Function’? [2]
4. What is ‘parameter passing’? Provide an example algorithm to support your answer.[2]
5. What is the difference between a parameter and an argument? [4]
6. Write an annotated algorithm to demonstrate how functions receive and return values. [4]
7. Discuss the use of parameter passing over the use of global variables in a program. [3]
Computer Science UK Membership Site Licence: Do not share outside your centre. 6
A-Level Computer Science
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
Computer Science UK Membership Site Licence: Do not share outside your centre.
7
_____________________________________________________
_____________________________________________________
A-Level Computer Science
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
Computer Science UK Membership Site Licence: Do not share outside your centre.
8
_____________________________________________________
A-Level Computer Science
State/Identify/Give/Name: Simply label a diagram, fill out a table or write a few words
Describe: Describing is ‘saying what you see’ (E.G.: A computer will have a CPU, Primary and Secondary storage etc)
Explain: Explaining is ‘saying WHY/HOW something is like that’. (E.G.: A computer will have a CPU so that it can process all of the data the
computer needs to perform a range of tasks. Primary and Secondary storage is needed because…)
Discuss: Discussing is ‘looking at two sides of an issue, weighing up the two views and giving a conclusion’. Often these require a mini essay
answer. (E.G.: New technology could be seen as being bad for the environment because…, but on the other hand, new technology has led to…
In conclusion I believe that…)
Describe/Explain/Discuss using examples: Finally, if you are asked to give examples in any of these types of questions – YOU MUST GIVE
EXAMPLES!
Further thoughts:
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
Computer Science UK Membership Site Licence: Do not share outside your centre. 10