170 likes | 286 Views
First, recall the code for finding a sum of the values read in: int g, sum = 0; for (int i = 0; i < n; i++) { cin >> g; sum += g; }. Finding the Minimum Value.
E N D
First, recall the code for finding a sum of the values read in: int g, sum = 0; for (int i = 0; i < n; i++) { cin >> g; sum += g; } Finding the Minimum Value
Finding the minimum value is similar. Instead of a variable to store the current sum, we need a variable to store the current minimum value: int g, m = ?; for (int i = 0; i < n; i++) { cin >> g; if(g < m) m = g; } Finding the Minimum Value (cont'd)
The only question left is: What should m be initialized to? A common mistake is to set m to be 0, just like the sum is. But suppose we are reading in grades between 0 and 100: Input: 100 70 80 30 90 m: 0 0 0 0 0 0 What should m be initialized to?
Instead of initializing m to 0, it should be set to the maximum value that could be read in (if known). In this example, m would be set to 100: Input: 100 70 80 30 90 m: 100 100 70 70 30 30 Correct Code
If the maximum value isn't known, and m is an int, we could use the constant MAXINT, which is the largest value that an int variable can have. An alternative is to read the first value before the loop, and use that to start off m: int g, m; cin >> g; m = g; for (int i = 1; i < n; i++) { cin >> g; if (g < m) m = g; } An Alternative
Instead of using the if-statement, we could use the min function from the previous class: for(int i=0; i < n; i++) { cin >> g; m = min(m,g); } This sets the new value of m to be the minimum of the old value of m and g. Other Code
A function can be declared without being defined at the same time. That is, we can say what the name of the function is, give its arguments and types, and the return type, without specifying the actual code for the function. This is called a function declaration or function prototype. int min(int a, int b); Note the semicolon at the end, rather than a body. Function Declaration
The declaration of a function must occur in the program before the function is called. But the definition of the function can occur later in the code. Even if you have a separate declaration, you must still define the function somewhere. The function definition should have the same header as the function declaration, but include code. Function Definition
int min(int a, int b); // function declaration int main(void) { // main program uses min int x = 5, y = 7; cout << min(x,y) << endl; } int min(int a, int b) { // function definition return (a < b ? a : b); } Example
Parameters, sometimes called formal parameters, are the variables that occur in function declaration. Arguments, sometimes called actual arguments, are the values that used in the call to the function. Parameters must be variables, because they are names of data objects. Arguments may be expressions (for now). Parameters vs. Arguments
Each argument is evaluated. For each parameter, a new data object is created and is label by the parameter. The value of the corresponding argument is the initial value of the data object. The body of the function is executed, and the return value (if any) is saved. When the function is done, the data objects for the parameters are destroyed. When you call a function
Arguments correspond to the parameters by their order. That is, the first argument corresponds to the first parameter argument to the second parameter, and so forth. Don't mix them up. The type of the argument should match that of the parameter (or be able to be automatically converted to that type, i.e., int -> double). Pitfalls
Functions may call other functions (but remember, a function must be declared before it can be used). Functions may have zero arguments. Functions may have the return type void. In this case, they are not used as part of an expression, but the function call is followed by a semicolon, to make it a statement. main is a function. More on Functions
Function foo may call function goo, and function goo may call function foo as well. A function may call itself. This is called recursion and is tricky. If your function calls itself (at this point), it's probably a mistake and you didn't mean to do it. Functions calling Functions
Functions may have local variables. You may declare a variable inside of a function. The data object created exists only while the function is running and disappears when the function is done. The label of the data object (that variable) may be the same name as one in the main program (or the calling function), but stands for a different data object. This can be confusing for novice programmers. Local Variables
int min(int a, int b); int main(void) { int x = 5; y = 7, c; c = min(x,y); cout << c << endl; } int min(int a, int b) { int c = (a < b ? a : b); return c; } Example
Write a function, getMin, with one int parameter, n, that reads in n grades (between 0 and 100), and returns the lowest grade read in. The function should call the min function. The return value should be of type int. Exercise