170 likes | 286 Views
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
E N D
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.
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;
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.
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.
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; } }
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.
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);
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
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)); ...
Random Example Output ./random 2 ./random 9 ./random 4 ./random Queen
Exercise Extend the program to print out the suit as well as the number. The suit should be chosen randomly.
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.
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.
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.
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.
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; }