Title: ICOM 4015 Advanced Programming
1ICOM 4015 Advanced Programming
- Lecture 3
- Procedural Abstraction I
- Reading Chapter 3
Prof. Bienvenido Velez
2Lecture Series on Procedural Abstraction
- Lecture 1
- Functions as abstract contracts
- Value/reference parameters
- Modules and separate compilation
- Lecture 2
- Scoping rules
- Functional arguments
- Lecture 3
- Top-down modular design
- Lecture 4
- Recursive functions
- Lecture 5
- Functions overloading and templates
- Lecture 6
- More examples of procedural abstraction
3Procedural Abstraction I Outline
- Functions as abstract contracts
- Value/Reference parameters
- Procedural Abstraction Defined
- Separate compilation
4Example 0Finding the roots of ax2 bx c
include ltcmathgt // roots(a, b, c, r1, r2) -
returns the number of // real roots of ax2 bx
c. If two roots exists // they are returned is
r1 and r2. If only one root // exists, it is
returned in r1. Otherwise the value // of r1 and
r2 is undetermined. int roots(float a, float b,
float c, float r1, float r2) float d b
b - 4.0 a c if (d lt 0)
return 0 r1 (-b sqrt(d)) / (2.0
a) if (d 0) return 1
r2 (-b - sqrt(d)) / (2.0 a) return
2
WHAT?
formal parameters
HOW?
roots.cc
definitions
int roots(float a, float b, float c,float r1,
float r2)
roots.h
declarations
5Procedural Abstraction
- A function should accomplish ONE well defined and
easy to remember task - A function establishes a contract between callers
and implementers - The implementer may select any implementation
that satisfies the contract. - The contract should specify WHAT task the
function accomplishes, NOT HOW it accomplishes it
HOW is hidden or abstracted out, hence the
name procedural abstraction
6Layered Software Design
GUI
Text-based Batch Interface
Text-based Interactive Interface
7UI ModulePolynomial Roots APPlication
// rootsUI.cc // Simple text-based interactive
user interface for // finding the roots of
polynomials  include ltiostreamgt  void
readCoefficients(float a, float b, float c)
cout ltlt "Enter coefficient for cuadratic term
(a) " cin gtgt a cout ltlt "Enter coefficient
for linear term (b) " cin gtgt b cout ltlt
"Enter coefficient for constant term (c) "
cin gtgt c  void reportRoots(float a, float b,
float c, int numRoots, float root1, float
root2) cout ltlt "a " ltlt a ltlt " b "
ltlt b ltlt " c " ltlt c ltlt " "
switch (numRoots) case 0 cout ltlt "No
real roots" ltlt endl break case 1
cout ltlt "One real root " ltlt root1 ltlt endl
break case 2 cout ltlt "Two real roots
" ltlt root1 ltlt " and " ltlt root2 ltlt endl
break default cout ltlt "Error bad
number of roots" ltlt endl
rootsUI.cc
8Main Module Polynomial Roots App
// rootsMain.cc // Main module for finding the
roots of polynomials  include
"roots.h" include "rootsUI.h" Â int main()
float a,b,c readCoefficients(a, b, c) float
root1, root2 int numRoots roots(a, b, c,
root1, root2) reportRoots(a, b, c, numRoots,
root1, root2)
rootsMain.cc
arguments or actual parameters
9Complete Layered Application
MAIN rootsMain.cc
rootsUI.h
roots.h
10Separate Compilation
source files
roots.cc
rootsUI.cc
rootsMain.cc
compile
compile
compile
compile time
object files
roots.o
rootsUI.o
rootsMain.o
compile
link time
executable files
roots
run time
11Summary of Concepts
- The problem of developing user interfaces can
often be separated in a software development
project. - Value parameters changes remain local to
function - Reference parameters changes propagate to
argument - Procedural abstraction a function establishes a
contract with its callers on what it
accomplishes, hiding how it accomplishes it. - Modules and separate compilation provide an
additional level of granularity for procedural
abstraction.