Chapter 4 Procedural Abstraction and Functions That Return a Value.
Procedural Abstraction and Functions · 8 Abstraction and Functions 13 Procedural Abstraction in...
Transcript of Procedural Abstraction and Functions · 8 Abstraction and Functions 13 Procedural Abstraction in...
1
8 Abstraction and Functions 1
Procedural Abstraction and!
Functions"1E3 !
Topic 8!
8 Abstraction and Functions 2
Objectives"
n This topic should allow students to!n Understand the importance of abstraction in
programming!n Recognise when a function would be useful.!
2
8 Abstraction and Functions 3
The plan"
n Today we’ll introduce the idea of procedural abstraction and look at some examples.!
n Next topic will cover the detail and get you to start using functions.!
8 Abstraction and Functions 4
Abstraction"n In computer science abstraction means
identifying and naming something complex and then using that name to refer to the complex thing.!n Data abstraction : !
n design a data structure to hold details about a bank account; call it bank_account!
n Procedural abstraction:!n Give a name to a piece of code that does something; use the
name to get that something done.!
3
8 Abstraction and Functions 5
Abstraction contd./"n Imagine this scenario: every time we start a lecture I request the
following sequence of actions:!n come in please.!n turn on the lights.!n turn on the computer.!n turn on the projector.!n everyone have a seat.!n get out your copybooks.!n get out your pens.!
8 Abstraction and Functions 6
Abstraction contd./"n Imagine this scenario: every time we start a lecture I request the
following sequence of actions:!n come in please.!n turn on the lights.!n turn on the computer.!n turn on the projector.!n everyone have a seat.!n get out your copybooks.!n get out your pens.!n Wouldn’t it be easier if we just define this procedure once
and then give it a simple name by which we invoke it? !n “Let’sStart ( )”"
4
8 Abstraction and Functions 7
Abstraction contd./"n What are the benefits of using Let’sStart ()?!
n Abstraction"n short name, less details in main stream of lecture.!
8 Abstraction and Functions 8
Abstraction contd./"n What are the benefits of using Let’sStart ()?!
n Abstraction"n short name, less details in main stream of lecture.!
n Single point of editing"n imagine if I’ve written this action sequence at the beginning of every lecture’s
powerpoint, then one day we came to the point where you all work on iPads and I no more want to tell you: get out your copybooks and pens;!
n in how many slides will I have to change those lines?!n if we had agreed on the procedure and put it on the wall here, I could just
edit it in one place and get over with it.!
5
8 Abstraction and Functions 9
Abstraction contd./"n What are the benefits of using Let’sStart ()?!
n Abstraction"n short name, less details in main stream of lecture.!
n Single point of editing"n imagine if I’ve written this action sequence at the beginning of every lecture’s
powerpoint, then one day we came to the point where you all work on iPads and I no more want to tell you: get out your copybooks and pens;!
n in how many slides will I have to change those lines?!n if we had agreed on the procedure and put it on the wall here, I could just
edit it in one place and get over with it.!n Single point of error-checking and debugging!
n if you sense an error, you only need to review this code in one place.!
8 Abstraction and Functions 10
Abstraction contd./"n What are the benefits of using Let’sStart ()?!
n Abstraction"n short name, less details in main stream of lecture.!
n Single point of editing"n imagine if I’ve written this action sequence at the beginning of every lecture’s
powerpoint, then one day we came to the point where you all work on iPads and I no more want to tell you: get out your copybooks and pens;!
n in how many slides will I have to change those lines?!n if we had agreed on the procedure and put it on the wall here, I could just
edit it in one place and get over with it.!n Single point of error-checking and debugging!
n if you sense an error, you only need to review this code in one place.!n Reusability"
n We could package this procedure and give it to others to use… just like we use some functions in C++ (e.g. getline, pow, sqrt, etc.)!
6
8 Abstraction and Functions 11
Procedural Abstraction"n Give a name to an algorithm !n Use the name to execute that algorithm within
another one.!n Example!
n procedure sqrt (x)"!{algorithm for computing sq. root}!
n sqrt (789.54)!n Means APPLY the sqrt procedure to the number 789.54.!
8 Abstraction and Functions 12
Procedural Abstraction"n Example 2!
n procedure sort (list) "!{algorithm for sorting a list}!
n sort (list-of-names)!n Means APPLY the sort procedure to the list of
names.!n Abstraction allows you to separate details
of how to carry out the procedure from the algorithm that uses the procedure. !
7
8 Abstraction and Functions 13
Procedural Abstraction in C++"n In C++ procedural abstractions are called
functions.!n A function encapsulates and names an algorithm
to solve a particular problem.!n That algorithm can be used in a program simply by
calling the function by name, generally supplying information and receiving a result.!
n sqrt, pow, setprecision are built-in functions that we have used.!
n We can design and implement our own functions as we need them.!
8 Abstraction and Functions 14
Some built-in functions"n By including <cmath> you can use !n pow(i,n)!
n A built-in function to raise i to the power of n. We used it in the investment table program.!
n sqrt(x), fabs(x), sin(x), cos(x)!n sqrt returns the square root.!n fabs returns the absolute value of a double!
n Note that you don’t have to worry about how these functions are coded.!
8
8 Abstraction and Functions 15
The role of functions"n Hide detail
n We don’t need to care how sqrt(x) is implemented.
n While we are writing the main payroll program we can ignore how gross_pay (hrs, rate) operates.
8 Abstraction and Functions 16
The role of functions"n Hide detail
n We don’t need to care how sqrt(x) is implemented.
n While we are writing the main payroll program we can ignore how gross_pay (hrs, rate) operates.
n Increase readability n is_a_factor_of(n,d) should be more meaningful
than (n%d == 0)
9
8 Abstraction and Functions 17
Role of functions cont./"n Allow reuse of code!
n sqrt function is useful in all sorts of mathematical programs.!
8 Abstraction and Functions 18
Role of functions cont./"n Allow reuse of code!
n sqrt function is useful in all sorts of mathematical programs.!
n Modular software development:!n Aids program design !
n Use a function to implement subtasks of an algorithm.!n Allows incremental development and testing!n Allows division of labour.!
10
8 Abstraction and Functions 19
Modular System Development"n Build software like you would build a car.!n Identify the components you need.!n Specify what each needs to do!n Buy one that meets the spec or get someone to
build one.!n Test each component before putting it in the
system.!n To improve the car, replace components with
better versions.!
8 Abstraction and Functions 20
Modular Software Development"
n Large programs must be broken down into components.!
n Each component is implemented by a function.!n Specify the behaviour of each component and
the interface between the system and the component.!
n Develop and test components separately before integrating into the whole.!