Our fourth style of function takes arguments AND returns a
value.
//Example program with
driver and function
//Screen display shown at the bottom
//A minimum value function
#include<iostream.h>
#include<stdlib.h>
int min(int x, int y);
//function
prototype
int main(void)
// driver program
{
system("CLS");
int test1 = 12, test2 = 10;
//arbitrary
test values
cout<<"The minimum of "<< test1
<< " and " << test2
<<
" is " << min(test1, test2) << ".\n";
return 0;
}
//function definition
int min(int x, int y)
{
int minimum;
if (x < y)
minimum = x;
else
minimum = y;
return (minimum);
} |
Alternative codings
for this function appear at the right. Remember, there are
always many ways to accomplish a task. |
Screen
Display:
The
minimum
of 12 and 10 is 10.
|