Some Predefined Functions (1 of 2) Display 3.2 Some Predefined Functions NAME DESCRIPTION TYPE OF TYPE OF EXAMPLE VALUE LIBRARY ARGUMENTS VALUE HEADER RETURNED sqrt Square double double sqrt(4.0) 2.0 cmath root pow Powers double double pow(2.0,3.0) 8.0 cmath abs Absolute int int abs(-7) > cstdlib value for abs(7) int labs Absolute long long 1abs(-70000) 70000 cstdlib value for 1abs(70000) 70000 1ong fabs Absolute double double fabs(-7.5) 7.5 cmath value for fabs(7.5) 7.5 double Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-11 Some Predefined Functions (1 of 2)
Some Predefined Functions(2 of 2) ceil Ceiling double double ceil(3.2) 4.0 cmath (round ceil(3.9) 4.0 up) floor Floor double double floor(3.2) 3.0 cmath (round floor(3.9) 3.0 down) exit End pro- int void exit(1); None cstdlib gram rand Random None int rand() Varies cstdlib number srand Setseed unsigned void srand(42); None cstdlib for rand int Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-12 Some Predefined Functions (2 of 2)
Predefined Void Functions ◆No returned value Performs an action,but sends no "answer" When called,it's a statement itself ◆exit(1);∥No return value,so not assigned .This call terminates program void functions can still have arguments All aspects same as functions that "return a value" ◆They just don't return a value! Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-13 Predefined Void Functions ¨ No returned value ¨ Performs an action, but sends no "answer" ¨ When called, it’s a statement itself ¨ exit(1); // No return value, so not assigned ¨ This call terminates program ¨ void functions can still have arguments ¨ All aspects same as functions that "return a value" ¨ They just don’t return a value!
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) Copyright 2006 Pearson Addison-Wesley.All rights reserved. 3-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 3-14 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)