1 / 17

Functions

Functions. A function takes zero or more values and returns a value. Functions may be predefined. Functions may be user-written. Examples of predefined functions are: sqrt(x) // square root of x abs(x) // absolute value of x rand() // returns a random number

Télécharger la présentation

Functions

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. Functions • A function takes zero or more values and returns a value. • Functions may be predefined. • Functions may be user-written. • Examples of predefined functions are: • sqrt(x) // square root of x • abs(x) // absolute value of x • rand() // returns a random number • Functions are used as terms in expressions.

  2. Predefined Functions

  3. Predefined Functions Example if(floor(x) < ceil(x)) cout << pow(x,2.0) << endl; if(rand()% 20 < 10) cout << “I win!” << endl; else cout << “You win!” << endl;

  4. exit(n) Function The exit(n) function is used to end the program and return a value to the caller, which is usually the operating system or C++ environment. Often the value is used to indicate whether the program terminated normally, exit(0), or if the program terminated in an error, exit(n), where n is an error code.

  5. Random numbers The rand() function is used to return (pseudo-) randomly generated numbers. The value returned is an int between 0 and RAND_MAX (a constant). This function can be used to create random effects.

  6. Random Example #include <iostream> #include <cstdlib> using namespace std; int main(void) { int number = rand() % 13 + 1; switch(number) { case 1: cout << “Ace” << endl; break; case 11: cout << “Jack” << endl; break; case 12: cout << “Queen” << endl; break; case 13: cout << “King” << endl; break; default: cout << number << endl; break; } }

  7. Example Run ./random Ace ./random Ace ./random Ace ./random Ace What's wrong with this output? Every time the program is run, it's the same.

  8. Random Number seed To have the random number generator create different numbers each run, you must start the process at a different point. You can supply a seed which indicates where the process of random number generation is to start. The srand(n) function is used to set the random seed. srand(10);

  9. Random Example re-visited #include <iostream> #include <cstdlib> using namespace std; int main(void) { srand(10); int number = rand() % 13 + 1; switch(number) { ... } ./random Queen ./random Queen

  10. Random Example Solution The solution is to set the random number generator seed to a different value each time the program is run. One way to do this is to use the value of the system clock, via the time(NULL) function. #include <ctime> int main(void) { srand(time(NULL)); ...

  11. Random Example Output ./random 2 ./random 9 ./random 4 ./random Queen

  12. Exercise Extend the program to print out the suit as well as the number. The suit should be chosen randomly.

  13. User-defined Functions • The programmer can declare functions as well. • Like predefined functions, these functions take a number of arguments (of given types) and return a value of some type. • The user-defined function may be used in expressions. • A user-defined function is declared (and defined) outside of the main function.

  14. User-defined Functions (cont'd) There are two (really three) parts of functions: • Declaring (and defining) the function • Using the function The declaration occurs outside of the main function. The function is used (called) anywhere inside of an expression.

  15. Function Declaration (and Defn) The programmer must specify: • The name of the function • The number and types of the arguments (parameters) • The type of the return value In addition to the declaration, the programmer usually gives the code of the function (the function body). This is the function definition.

  16. Function Declaration Example int max(int a, int b) { int c; if(a > b) c = a; else c = b; return c; } The return statement is used to return the value of the function.

  17. Function Usage Example In another function, for example, the main function, the max function is called as part of an expression: int main(void) { int x = 5, y = 10; int z = max(x,y); cout << z << endl; return 0; }

More Related