1 / 16

Today’s Lecture

Today’s Lecture. Predefined Functions. Introduction to Functions. Reuse Issue Building Blocks of Programs Two types of functions Predefined Programmer defined. Predefined Functions. Predefined in the libraries Example: Calculate the square root of a number

Download Presentation

Today’s Lecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Lecture Predefined Functions

  2. Introduction to Functions • Reuse Issue • Building Blocks of Programs • Two types of functions • Predefined • Programmer defined

  3. Predefined Functions • Predefined in the libraries • Example: • Calculate the square root of a number • double sqrt(double) is defined in library <cmath> • Three parts: • Return type • Function name • Argument list

  4. The Function Call • To use: • Must "#include" the library that defines the function <cmath> • Provide required arguments • Example: double root; root = sqrt (9.0); • The argument in a function call (9.0) can be a literal, a variable, or an expression

  5. A Larger Example: Display 3.1 A Predefined Function That Returns a Value (1 of 2)

  6. A Larger Example: Display 3.1 A Predefined Function That Returns a Value (2 of 2)

  7. Predefined Functions • Libraries full of functions for our use! • Two types: • Those that return a value • Those that do not (void) • Must "#include" appropriate library • e.g., • <cmath>, <cstdlib> (Original "C" libraries) • <iostream> (for cout, cin)

  8. More Predefined Functions • #include <cstdlib> • abs() // Returns absolute value of an int • pow(x, y) • Returns x to the power y • Notice this function receives two arguments • A function can have any number of arguments, of varying data types

  9. Even More Math Functions: Display 3.2 Some Predefined Functions (1 of 2)

  10. Even More Math Functions: Display 3.2 Some Predefined Functions (2 of 2)

  11. Predefined Void Functions • No returned value • Performs an action, but sends no "answer" • All aspects same as functions that "return a value" • They just don’t return a value! • Example • exit(int)

  12. Random Number Generator • Return "randomly chosen" number • Used for simulations, games • rand() • Takes no arguments • Returns value between 0 & RAND_MAX • Scaling • Squeezes random number into smaller range rand() % 6 • Returns random value between 0 & 5 • Shiftingrand() % 6 + 1 • Shifts range between 1 & 6 (e.g., die roll)

  13. Random Examples • Random double between 0.0 & 1.0:rand()/(double)(RAND_MAX) • Type cast used to force double-precision division • Random int between 1 & 6:rand() % 6 + 1 • "%" is modulus operator (remainder) • Random int between 10 & 20:rand() % 10 + 10

  14. Pseudorandom Numbers • The function rand() takes no arguments and returns a integer in the rage of [0, RAND_MAX] • Numbers appears to be random, but really not. • It is called pseudorandom numbers • The sequence of the random is determined by seed

  15. Pseudorandom Numbers • If you start rand with the same seed, you will produce the same sequence random number • To get true random number, use function srand to reset seed. void srand(int)

  16. Character Functions • Include <cctype> bool isdigit(char) bool isalpha(cha) bool isspace(char) bool islower(char) bool isupper(char) int tolower(char) int toupper(char)

More Related