ALevelComputing_SessionU28

advertisement
Session Objectives#U2 S8
COULD explain what is meant by recursion and the relative strengths between
recursion and iteration
SHOULD explain the need for parameters in defining subroutines and apply
subroutines to a given program (mulitcalc in Python
MUST identify the difference between a procedure and a function
Create recursive algorithm and compare an iterative version in Python.
Explain why one is recursive and the other is iterative.
A Level Computing#BristolMet
Key Words
A Level Computing#BristolMet
KEYWORDS EXPLAINED
Sub routines are sections of code which perform a specific task
which can be separated from the main program. They are called
either procedures or functions.
A function performs a specific task and returns a single value
i.e input 1 add input 2, output answer
A procedure performs a task or operation but does not return a
value i.e print file or open file
Parameters – when a function or procedure is defined information
about the data item(s) being supplied are given. This is known
as the parameters are usually expressed in (brackets) after the
identifier.
A Level Computing#BristolMet
PARAMETERS
Parameters are needed to tell the computer what data types are
being used for each variable in the subroutine, and in the case
of functions, the data type of the ‘return value’ (the single
value returned after the instructions have been carried out).
What do these tell us?
PROCEDURE Print Details (Name: String, Copies: Integer)
• The Procedure name is Print Details
• The parameter Name is of data type String
• The parameter Copies is of Integer
FUNCTION Interest(AMT:Currency, Rate:Real, Yrs:Integer):Currency
•
•
•
•
•
The function name is interest
Amt of data type currency
Rate of data type Real
Yrs of data type Integer
The return value of the function is of data type Currency
A Level Computing#BristolMet
FUNCTIONS
Functions need to be defined before they can be called (or used)
in the main body of the program.
In Python def function():
instructions…
is the method for defining the function and it can be called
again later in the program with a simple function()
TASK: Look at the addcalc.py example and then apply to your
multicalc.py
TASK 2: Compare your original program with the functional
version and discuss which you think is the more efficient.
Prepare to answer the questions of when or why should you use
functions in your program design.
A Level Computing#BristolMet
RECURSION
This is when a subroutine (procedure or function) calls itself.
The subroutine executes instructions as normal until it calls
itself, the subroutine then pauses and it goes through the
process again until the conditions set out in the recursive
subroutine are met – this is known as the stopping condition.
So in a recursive subroutine it is also iterative in nature in
the sense that it repeats itself until the stopping condition is
met.
See the example on p83-84 for further explanation and create a
table of strengths/weakness of iteration vs recursion (p.85)
Now answer the exam style questions on p85-86 (Complete for Hwk)
A Level Computing#BristolMet
Download