0% found this document useful (0 votes)
41 views

Chapter 6 - Handout

The document discusses predefined mathematical functions in C++ and user-defined functions. It provides examples of abs, max and palindrome checking functions. The abs and max functions demonstrate how to define and call simple functions that return values. The palindrome function illustrates recursively checking if a number is a palindrome by comparing left and right digits.

Uploaded by

Fawaz Alhamry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Chapter 6 - Handout

The document discusses predefined mathematical functions in C++ and user-defined functions. It provides examples of abs, max and palindrome checking functions. The abs and max functions demonstrate how to define and call simple functions that return values. The palindrome function illustrates recursively checking if a number is a palindrome by comparing left and right digits.

Uploaded by

Fawaz Alhamry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 6: User Defined Functions I

Predefined Functions
Header

Function

Purpose

cstdlib

abs(x)

cmath

cmath

cmath

Returns the absolute


value of x

Example
Statement
Result
abs(-7)
7
abs(7)
7

Parameters
Type
int

Result
Type
int

fabs(x)

Returns the absolute


value of x

fabs(-7.76)
fabs(7.67)

7.76
7.67

double

double

ceil(x)

Returns the value


rounded up to the next
higher integer

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)

Returns the value


rounded down to the
next lower integer

cmath

cos(x)

Returns the cosine of


angle x

cmath

exp(x)

Returns e to power x,
where e = 2.718

exp(1.0)

2.718

double

double

cmath

pow(x,y)

Returns x to the power


of y

pow(0.16, 0.5)

0.4

double

double

cctype

tolower(x)

Returns the lower case


of x

static_cast<char>
(tolower(A))

int

int

int

int

a
static_cast<char>
(tolower(a))

cctype

toupper(x)

Returns the upper case


of x

static_cast<char>
(toupper(A))
static_cast<char>
(toupper(a))

A
A

Value returning functions


To define any function, we need to define the following:
1. Function name;
2. Number and type of the parameters
3. data type of the return value.
The return value, can be used in:
1. cout statement
2. assignment value
3. in some calculation
To define a function that calculates the absolute value of an integer, then we can do
it as follows:
#include<iostream>
using namespace std;
Output
// Define the function header
// Return-type functionName (parameters);
int abs( int x ) ;
3
int main( )
4
{
32
int a=3, b=-4, c;
77
//calling the function abs and save the returned
//value in c
c = abs (a);
cout<<c<<endl;
c=abs(b);
cout<<c<<endl;;
c=abs(-32);
cout<<c<<endl;
c = abs(a) + abs(b);
cout<<c;
//calling the function abs and use the returned
//value in cout statement
cout<< abs(a) + abs(b) << endl;
return 0;
}
//function Implementation
int abs (int x)
{
if ( x<0) x*=-1;
return x;
}

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;

double Max( double a, double b )


{
if (a>b)
return a;
return b;
}

What is the output?


#include<iostream>
using namespace std;
double Max( double a, double b ) ;
double MaxThree (double a1, double a2, double a3) ;

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) ) );
}

Enter three double values:


12 34 56
The largest value is: 56

What is the output?


#include <iostream>
#include <cmath>
using namespace std;
bool isNumPalindrome ( );
int main( )
{
bool result;
result = isNumPalindrome( );
if (result == true)
cout<<"The number is Palindrome";
else
cout<<"The number is Not Palindrome";
system("pause");
return 0;
}
bool isNumPalindrome( )
{
int num=121;
int pwr = 0;
int temp = num;
int left, right, tenTopwr;
if (num < 10)
return true;
else
{
while ( temp >=10 )
{ temp = temp / 10;
pwr++;
}
while (num >= 10)
{
tenTopwr = static_cast<int>(pow(10.0, pwr));
left = num / tenTopwr;
right = num%10;
if ( left != right )
return false;
else
{
num = num % tenTopwr;
num = num / 10;
pwr = pwr - 2;
}
}//end while
return true;
}//end else
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy