Introduction to Computer Programming with MATLAB
CEE/MAE M20
Lecture 5a
Abstraction
: functions separate interface from implementation
CEE/MAE M20
Week 5 Reading in ITC
2
CEE/MAE M20
Two huge concepts
Interface
What
job gets done?
What goes in?
What comes out?
3
Implementation
How
does the job get done?
How
are outputs determined
from inputs?
CEE/MAE M20
Why do we need functions?
1.
Elevates reasoning by hiding details.
2.
Facilitates topdown design.
3.
Software management.
4
Insight Through Computing
CEE/MAE M20
Elevates Reasoning
Example: Solving the quadratic equation
•
When designing a quadratic equation solver, we need to
compute the
square root
of the discriminant.
•
We could build in the algorithm that computes square root.
•
But this is distracting from the core function of the routine.
•
Instead, it is nice to have a
sqrt
function that masks all of this
unnecessary detail.
5
Insight Through Computing
CEE/MAE M20
Elevates Reasoning
Example: Solving the quadratic equation
•
We get to think at the higher level of dealing with
ax
2
+ bx + c = 0
•
Easier to understand the quadratic equation solving code:
:
r1 = (b+sqrt(b^24*a*c))/(2*a);
r2 = (bsqrt(b^24*a*c))/(2*a);
:
6
Insight Through Computing
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
CEE/MAE M20
Facilitates TopDown Design
7
Insight Through Computing
Example: Drawing the Colonial ﬂag
CEE/MAE M20
Facilitates TopDown Design
1. Top level:
Focus on how to draw the ﬂag, given just a
specification
of what the
functions DrawRect and DrawStar do.
For example, consider the pseudocode for drawing the 7 short
stripes:
8
Insight Through Computing
Set length L and width W for short stripe
Set x and y for lower left corner of top stripe
Set color c = ‘r’ (red)
for
k = 1:7
Draw rectangle at (x,y) of length L and width W with color c
Subtract W from y
Set c to ‘w’ (white) if k is odd or to ‘r’ (red) if k is even
end
CEE/MAE M20
Facilitates TopDown Design
1. Top level:
Focus on how to draw the ﬂag, given just a
specification
of what the
functions DrawRect and DrawStar do.
The actual MATLAB code for this instruction does not look much
different
9
Insight Through Computing
Set length L and width W for short stripe
Set x and y for lower left corner of top stripe
Set color c = ‘r’ (red)
for
k = 1:7
DrawRect(x,y,L,W,c);
Subtract W from y
Set c to ‘w’ (white) if k is odd or to ‘r’ (red) if k is even
end
CEE/MAE M20
Facilitates TopDown Design
2. Implementation:
Figure out how to
implement
DrawRect and DrawStar.
The actual procedure for drawing rectangles and stars is hidden
from user.
10
Insight Through Computing
CEE/MAE M20
To Specify a Function…
You describe how to use it, e.g.,
function
DrawRect(a,b,L,W,c)
% Adds rectangle to current window.
% Assumes hold is on. Vertices are
% (a,b),(a+L,b),(a+L,b+W), & (a,b+W).
% The color c is one of ‘r','g',
%'y','b','w','k','c',or 'm'.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '06
 ArunPrakash
 Computer Science, Computer Programming, Quadratic equation, CEE/MAE M20

Click to edit the document details