lect11 - DS CS 11002 9 Computer Sc Engg IIT Kharagpur 6 1...

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % Function Abstraction Lect 11 Goutam Biswas
Image of page 1

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % sin x We have already seen how to compute an approximate value of sin x from the following series: sin x = x - x 3 3! + x 5 5! - x 7 7! + x 9 9! · · · Lect 11 Goutam Biswas
Image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % sin x The computation of sin x gives a value like an expression. The value of sin x depends on the parameter x . If we can encapsulate and abstract the code for this computation, it can be used with different parameters in a program. Lect 11 Goutam Biswas
Image of page 3

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % Function Abstraction Encapsulated code is called a function in C language. A function has a name , zero or more parameters with their types a , the type of the return value b and the body of the code for computation. a These are called formal parameters b C function may have return type void . It does not return any value. The purpose of such a function is abstraction of a computation that causes side-effect. Lect 11 Goutam Biswas
Image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % Code for sin x xRadian = M_PI*x/180.0 ; term = xRadian ; sineVal = term ; termNo = 1 ; do { double factor ; factor = 2.0 * termNo++ ; factor = factor * (factor + 1.0) ; factor = - xRadian * xRadian / factor ; sineVal = sineVal + (term = factor * term) ; compError = 100.0*fabs(term/sineVal) ; } while (compError >= precError) ; Lect 11 Goutam Biswas
Image of page 5

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % Function Interface The interface of the function to the other part of the program is as follows: double mySin( double, double ); the name is mySin , there are two formal parameters, both are of type double ; one for the angle in degree and the other for the percentage error, the type of the return value is double . Lect 11 Goutam Biswas
Image of page 6
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 & $ % Function Definition mySin() #include <math.h> #define ABS(X) (((X) < 0.0) ? -(X) : (X)) double mySin(double x, double precError){ double xRadian, term, sineVal, compError ; int termNo ; xRadian = M_PI*x/180.0 ; term = xRadian ; sineVal = term ; termNo = 1 ; do { double factor ; factor = 2.0 * termNo++ ; factor = factor * (factor + 1.0) ; Lect 11 Goutam Biswas
Image of page 7

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 & $ % factor = - xRadian * xRadian / factor ; sineVal = sineVal + (term = factor * term) ; compError = 100.0*ABS(term/sineVal) ; } while (compError >= precError) ; return sineVal ; } // sin1.c Lect 11 Goutam Biswas
Image of page 8
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9 & $ % Note The name of the formal parameters are x and precError . The variables x, precError, xRadian, term, sineval, termNo are local to the function mySin() . They are not visible to the other parts of the program. The variable factor is local to the statement-block of the do-while loop and is not visible outside it. Lect 11 Goutam Biswas
Image of page 9

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10 & $ % Note The body of main() no more contains the code for sine computation. It invokes ( calls ) the mySin() function with the actual parameters .
Image of page 10
Image of page 11
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