lecture5a - Introduction to Computer Programming with...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 top-down 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^2-4*a*c))/(2*a); r2 = (-b-sqrt(b^2-4*a*c))/(2*a); : 6 Insight Through Computing
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CEE/MAE M20 Facilitates Top-Down Design 7 Insight Through Computing Example: Drawing the Colonial flag CEE/MAE M20 Facilitates Top-Down Design 1. Top level: Focus on how to draw the flag, 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 Top-Down Design 1. Top level: Focus on how to draw the flag, 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 Top-Down 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'.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern