Procedures & Functions (Cambridge (CIE) IGCSE Computer Science)
Revision Note
Written by: Robert Hampton
Reviewed by: James Woodhouse
What are functions and procedures?
Functions and procedures are a type of sub program, a sequence of instructions that perform a specific task or set of tasks
Procedures and functions are defined at the start of the code
Sub programs are often used to simplify a program by breaking it into smaller, more manageable parts
Sub programs can be used to:
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)
ORPROCEDURE TaxCalculator(pay,taxcode)
Sub programs can have multiple parameters
To use a sub program you 'call' it from the main program
What's the difference between a function and procedure?
A Function returns a value whereas a procedure does not
Procedures
Procedures are defined using the PROCEDURE keyword in pseudocode or def keyword in Python
A procedure can be written as:
PROCEDURE <identifier>
<statements>
ENDPROCEDUREOr if parameters are being used:
PROCEDURE <identifier>(<param1>:<data type>, <param2>:<data type>...)
<statements>
ENDPROCEDURE
Creating a procedure |
---|
Pseudocode |
|
Python |
|
To call a procedure, it can be written as:
CALL <identifier>
CALL <identifier>(Value1,Value2...)
Calling a procedure |
---|
Pseudocode |
|
Python |
|
Examples
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 |
---|
# Procedure definition
# Outputs the option
# Procedure definition
# Main program |
Functions
Functions are defined using the FUNCTION keyword in pseudocode or def keyword in Python
Functions always return a value so they do not use the CALL function, instead they are called within an expression
A function can be written as:
FUNCTION <identifier> RETURNS <data type>
<statements>
ENDFUNCTIONOr if parameters are being used:
FUNCTION <identifier>(<param1>:<data type>, <param2>:<data type>...) RETURNS <data type>
<statements>
ENDFUNCTION
Creating and using a function |
---|
Pseudocode |
// Output the value returned from the function
|
Python |
// Output the value returned from the function
|
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 |
---|
# Function definition, length and width are parameters
# Main program #1 # Main program #2 |
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 :
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 :
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 | Python |
A |
|
B |
OR
|
Last updated:
You've read 0 of your 10 free revision notes
Unlock more, it's free!
Did this page help you?