Chapter 6 - Handout
Chapter 6 - Handout
Predefined Functions
Header
Function
Purpose
cstdlib
abs(x)
cmath
cmath
cmath
Example
Statement
Result
abs(-7)
7
abs(7)
7
Parameters
Type
int
Result
Type
int
fabs(x)
fabs(-7.76)
fabs(7.67)
7.76
7.67
double
double
ceil(x)
ceil(56.34)
57.00
double
double
ceil(23.98)
24.00
ceil(-2.3)
floor(56.34)
-2.00
56.00
double
double
floor(45.67)
45.00
floor(-2.3)
cos(0.0)
-3
1.0
double
double
floor(x)
cmath
cos(x)
cmath
exp(x)
Returns e to power x,
where e = 2.718
exp(1.0)
2.718
double
double
cmath
pow(x,y)
pow(0.16, 0.5)
0.4
double
double
cctype
tolower(x)
static_cast<char>
(tolower(A))
int
int
int
int
a
static_cast<char>
(tolower(a))
cctype
toupper(x)
static_cast<char>
(toupper(A))
static_cast<char>
(toupper(a))
A
A
Max function:
Write a function Max, that takes as parameters two integers. The function should return
the value of the maximum value of the two integers.
#include<iostream>
using namespace std;
//define the function header
// return-type functionName (parameters);
double Max( double a, double b ) ;
int main( )
{
double x, y, large;
cout<<"Enter two double values:";
cin>>x>>y;
large = Max(x,y);
cout<<"The largest value is:"<<large;
system("pause");
return 0;
}
//function Implementation
double Max( double a, double b )
{
double M;
if (a>b)
M=a;
else
M=b;
return M;
}
We can also write the max function as follows:
double Max( double a, double b )
{
double M;
if (a>b) return a;
else return b;
}
Note: Illegal statements
X = Max (int x, 29);
Cout<< int Max(int x, int y);
double Max (double a1, a2)
return x, y;
int main( )
{
double x, y, z;
cout<<"Enter three double values:";
cin>>x>>y>>z;
cout<<"The largest value is:"<<MaxThree(x,y,z);
system("pause");
return 0;
}
double Max( double a, double b )
{
double M;
if (a>b) M=a;
else
M=b;
return M;
}
double MaxThree (double a1, double a2, double a3)
{
return ( Max(a1, Max(a2,a3) ) );
}