Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.

Similar presentations


Presentation on theme: "Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer."— 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  Three parts:  Return type  Function name  Argument list

4 The Function Call  To use:  Must "#include" the library that defines the function  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., , (Original "C" libraries)  (for cout, cin)

8 More Predefined Functions  #include  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  Shifting rand() % 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 bool isdigit(char) bool isalpha(cha) bool isspace(char) bool islower(char) bool isupper(char) int tolower(char) int toupper(char)


Download ppt "Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer."

Similar presentations


Ads by Google