{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

l05_6up - Pure and Eectful Functions CSci 1113 C/C for...

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

View Full Document Right Arrow Icon
: CSci 1113 C/C++ for Scientists and Engineers Section 010 - evening class Functions Prof. Eric Van Wyk and Ted Kaminski Spring 2010 c Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 1 / 18 Pure and Effectful Functions : Sample functions Mathematics double x = cos(0.34) ; double p = pow(2.0,5.0); Random numbers srand(seed); Seeds the pseudo-random number generator. r = rand(); Returns the next pseudo-random number. Need to include <cstdlib> . c Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 2 / 18 Pure and Effectful Functions : How can we modify Random.cpp to generate random integer numbers between a specified range? Say, between 1 and some value n ? How can we modify it to generate floating point random numbers in the range [0,1] ? http://www.cplusplus.com is a reasonable reference. See also http://www.cppreference.com/ . c Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 3 / 18 Pure and Effectful Functions : Exercise 5.1 Modify your square root program to assign a value to guess randomly. What effect does this have on the behavior of your program? c Van Wyk and Kaminski CSci 1113: C/C++ for Scientists and Engineers, evening class Spring 2010 Page 3 / 18 Pure and Effectful Functions : Pure and impure functions Most functions are pure functions . A pure function always returns the same result when given the same input and have now effect on program variables. (Except when the result of the function call is assigned to a variable.) For example, cos and pow are pure functions. Both srand and rand are not pure, or impure .
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
Image of page 2
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