T1 Procedures and Functions
T1 Procedures and Functions
Starter
OUTPUT "menu"
• A computer program OUTPUT "1: New file"
needs a menu to appear OUTPUT "2: Edit file"
OUTPUT "3: Delete file"
• A programmer writes the OUTPUT "4: Quit"
OUTPUT "5: Show menu"
code shown on the right
• What is wrong with this code? choice ← USERINPUT
IF choice = "5"
• What alternative could
THEN
be used? OUTPUT "1: New file"
OUTPUT "2: Edit file"
OUTPUT "3: Delete file"
OUTPUT "4: Quit"
OUTPUT "5: Show menu"
ENDIF
Procedures and functions
Unit 10 Advanced programming and databases
Starter
OUTPUT "menu" OUTPUT "1: New file"
OUTPUT "1: New file" OUTPUT "2: Edit file"
OUTPUT "2: Edit file" OUTPUT "3: Delete file"
OUTPUT "3: Delete file" OUTPUT "4: Quit"
OUTPUT "4: Quit" OUTPUT "5: Show menu"
OUTPUT "5: Show menu"
choice ← USERINPUT
IF choice = "5"
THEN
ENDIF
• What is wrong with this code? • What alternative could be
used?
• The programmer has copied and Subroutines – in this case a
pasted parts of the code which can procedure
lead to mistakes being introduced if
one part is corrected and not the
other
Procedures and functions
Unit 10 Advanced programming and databases
Using subroutines
• As your programs become larger and more complex,
they need to be broken down into smaller,
self-contained sections
• Here is an illustration of the concept:
Make a lemon
meringue pie
Built-in subroutines
• All programming languages have built-in procedures
and functions to perform common tasks
• You have like already used built-in subroutines such as these
in Python:
print("Hello")
input("Type in your name: ")
random(1,6)
• For each of the above subroutines, explain whether
they are functions or procedures
Procedures and functions
Unit 10 Advanced programming and databases
Built-in functions
print("Hello") # a procedure to output
text
input("Type in your name: ") # a function that
returns
# what the user enters
random(1,6) # a function that
returns an
# integer between 1 and
6
Built-in functions
print("Hello") # a procedure to output
text
input("Type in your name: ") # a function that
returns
# what the user enters
random(1,6) # a function that
returns an
# integer between 1 and
6
Racing game
showMenu procedure
PROCEDURE showMenu
OUTPUT " Menu "
OUTPUT "=================="
OUTPUT "1: Play game"
OUTPUT "2: Show key controls"
OUTPUT "3: High scores"
OUTPUT "4: Return to main menu"
OUTPUT "5: Exit game"
ENDPROCEDURE
This calls the procedure
CALL showMenu named showMenu
showKeys procedure
PROCEDURE showKeys
OUTPUT "Keyboard controls"
OUTPUT "================="
OUTPUT "1: Up arrow - forwards"
OUTPUT "2: Down arrow – backwards/brake"
OUTPUT "3: Left arrow – turn left"
OUTPUT "4: Right arrow – turn right"
OUTPUT "5: R - refuel“
ENDPROCEDURE
CALL showKeys
• Both showKeys and showMenu have no parameters
Procedures and functions
Unit 10 Advanced programming and databases
Worksheet 1
• Now complete Task 1 on Worksheet 1
Procedures and functions
Unit 10 Advanced programming and databases
Functions
• Functions work just like procedures, except at the
end they will return a value
FUNCTION sum(a, b)
total ← a + b
This is a function as it
RETURN total
returns a value
ENDFUNCTION
answer ← sum(5, 3)
OUTPUT answer
Functions
FUNCTION sum(a, b) • First sum(5,3) calls the sum function
• The values 5 and 3 are passed to
total ← a + b
the sum function
RETURN total • The sum function adds 5 + 3 and
ENDFUNCTION stores 8 in total
• The sum function returns the result
answer ← sum(5, 3) of 8 to the function call
OUTPUT answer • This is then assigned to the variable
called answer
• answer (which contains 8) is then
• Output: 8 output
• Write a function named ‘greatest’ that has three
parameters which are numbers
• The function needs to return the greatest of the
three numbers
Procedures and functions
Unit 10 Advanced programming and databases
Functions
FUNCTION greatest(a, b, c)
IF a > b AND a > c
THEN
RETURN a
IF b > a AND b > c
THEN
RETURN b
ELSE
RETURN c
ENDFUNCTION
Procedures and functions
Unit 10 Advanced programming and databases
Passing parameters
• When you create a function with the statement:
sum(a, b)
• a and b are known as parameters
• Data types can be given for each parameter:
sum(a : REAL, b : REAL) RETURNS REAL
Try this problem:
An average score is calculated by dividing a total
score by the number of attempts
• Create a function to calculate an average score
Procedures and functions
Unit 10 Advanced programming and databases
Worksheet 1
• Now complete Task 2 and Task 3 on Worksheet 1
Procedures and functions
Unit 10 Advanced programming and databases
More advantages of
using subroutines
• Subroutines can be stored in a subroutine library
and used in different programs if required
• Program maintenance is easier – if requirements
change then just the affected subroutines need to be
modified
Procedures and functions
Unit 10 Advanced programming and databases
Variable scope
• Variables var1 and var2 Variables var1, var2
are global variables and FUNCTION s1()
can be seen anywhere in variables a, b, c
the program ENDFUNCTION
Variable scope
• Think of each function as being a house with
reflective windows – you can see out, but no one
outside can see the variables in the ‘house’
Procedures and functions
Unit 10 Advanced programming and databases
Variable scope
ENDPROCEDURE
#Main program
This isn’t affected by anything happening in the
num ← 5 changeNum subroutine
Example
• Identify the local variables in function calcmax
FUNCTION calcmax(heights)
max ← heights[1]
n ← LENGTH(heights)
FOR count ← 1 TO n
IF heights[count] > max
THEN
max ← heights[count]
ENDIF
NEXT n
RETURN max
ENDFUNCTION
• What type of data structure is heights?
• How will the function be called?
Procedures and functions
Unit 10 Advanced programming and databases
Answer
• max, n and count are all local variables
FUNCTION CalcMax(heights) // heights is an array
max ← heights[1]
n ← LENGTH(heights)
FOR count ← 1 TO n
IF heights[count] > max
THEN
max ← heights[count]
ENDIF
NEXT count
RETURN max
ENDFUNCTION
• The function is called and the result stored with a
statement such as:
tallest ← CalcMax(pupilHeights)
• The value max will be returned to tallest
Procedures and functions
Unit 10 Advanced programming and databases
Advantages of using
local variables
• Local variables keep a subroutine self-contained,
so it can be used in any program without variable
names conflicting with those used in the calling
program
• Generally, you should use local variables wherever you
possibly can
Procedures and functions
Unit 10 Advanced programming and databases
Worksheet 1
• Now complete Task 4 on Worksheet 1
Procedures and functions
Unit 10 Advanced programming and databases
Plenary
• Answer the following questions in pairs
• What are two types of subroutine used in programming?
• How many parameters can functions and procedures have?
• Explain the difference between a local and global variables
• What are three reasons that you should try to use subroutines
where possible?
Procedures and functions
Unit 10 Advanced programming and databases
Plenary
• Two types of subroutine used in programming:
• Functions and procedures
Copyright
This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.
Licence agreement
This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.
The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.