1.2.11 Procedures and Functions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

A-Level Computer Science

Procedures and Functions


Introduction
As we start writing larger programs with more and more lines of code, finding errors and editing the code
can become more and more difficult. We can get lost in the hundreds of lines of code. To overcome this,
‘sensible programmers’ make use of sub routines.

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

Global and Local


 A global variables is one which is defined in the main program.
 A local variable is one which is defined in the procedure / function.

Here the local variable is


being printed (due to the
procedure call).
Then the global variable
is being printed (due to the
print statement).

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’.

Parameter Passing (passing values into subroutines)


As we have already seen, we can use subroutines to better organise our code so that finding errors and
editing the code can become easier. This is because major sections of our code is self-contained and separate
from one another One problem with the way we have previously written subroutines is that the data inside it
stays inside it – these types of subroutine are called procedures.
If I wanted to write a program in python, which printed the 10 times table, I might write a procedure like
this:

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.

The procedure above


now takes on the value passed to it, from the
main program. A value is being passed into it
for processing. This value is known as an
argument and this process is called parameter
passing
Important to realise that the variable name of
the value being passed doesn’t have to match
the destination variable name.

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).

Passing and Returning more than one value (argument)


In this example, the
arrows show how 2
arguments are being
passed from the main
program into the
functions parameter ‘x’
and ‘y’. It is important to
realise that the names of
the parameters
(containing the
arguments being passed)
and the receiving
parameter names, do not have to match. However, the order in which they are sent is the order in which they
are received (num1 is sent to x, num2 is sent to y).
In this example, the arrow highlighting the ‘return’ is showing how 2 pieces of data can be returned into the
main program. Of course many more could be returned in one go in this same manner.
In this python
example, as these
values are being
returned to one
variable ‘answer’, the
variable automatically
becomes a tuple,
which is a data
structure that can hold
a number of different
values (like a ‘list’, but
tuples are not editable
– they are immutable)

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

Checklist: Keywords / Key Terms:


 Date and title, clearly presented Subroutine: A self-contained, mini program within a larger program, which sits
 Spelling & grammar checked in the background waiting to be called/executed.
Procedure: A subroutine which doesn’t return data back to the main program.
 Question numbers in the margin Function: A subroutine which does return data back to the main program.
 Handwriting neat & legible
 Punctuation / Capital letters

_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
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!

Stick answer sheet here

Reflections: Score: / Percentage: % Grade: Progress: On / Above / Below

What Went Well?:  My answers effectively incorporated technical terminology.


 I demonstrated a good level of understanding.  My responses were well structured / organised.
 I responded to the command words effectively.  My revision strategy was effective as I showed depth of understanding in my
answers.
 My answers were detailed / were written in depth.
 My answers contained enough points / examples / explanations to achieve the
 My work was well presented / legible.
marks available.

Even Better If…:  I must incorporate key terminology into my answers.


 I must better organise my answers to improve its clarity.
 My answers need to be more accurate.
Computer Science UK Membership Site Licence: Do not share outside your centre. 9
 I must respond correctly to the command words.
 My answers need more detail / greater depth.
A-Level Computer Science

 I need to improve my revision strategy, as I did not demonstrate a depth of


understanding in my answers.
 My answers didn’t contain enough points / examples / explanations to
achieve the marks available.

Further thoughts:
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________

Computer Science UK Membership Site Licence: Do not share outside your centre. 10

You might also like