chapter 6 - handout

5
Chapter 6: User Defined Functions I Predefined Functions Example Header Function Purpose Statement Result Parameters Type Result Type cstdlib abs(x) Returns the absolute value of x abs(-7) abs(7) 7 7 int int cmath fabs(x) Returns the absolute value of x fabs(-7.76) fabs(7.67) 7.76 7.67 double double cmath ceil(x) Returns the value rounded up to the next higher integer ceil(56.34) ceil(23.98) ceil(-2.3) 57.00 24.00 -2.00 double double cmath floor(x) Returns the value rounded down to the next lower integer floor(56.34) floor(45.67) floor(-2.3) 56.00 45.00 -3 double double cmath cos(x) Returns the cosine of angle x cos(0.0) 1.0 double double 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’)) static_cast<char> (tolower(‘a’)) a a int int cctype toupper(x) Returns the upper case of x static_cast<char> (toupper(‘A’)) static_cast<char> (toupper(‘a’)) A A int int

Upload: zainabcom

Post on 20-Oct-2015

7 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 6 - Handout

Chapter 6: User Defined Functions I Predefined Functions

Example Header Function Purpose Statement Result

Parameters Type

Result Type

cstdlib abs(x) Returns the absolute value of x

abs(-7) abs(7)

7 7

int int

cmath fabs(x) Returns the absolute value of x

fabs(-7.76) fabs(7.67)

7.76 7.67

double double

cmath ceil(x) Returns the value rounded up to the next higher integer

ceil(56.34) ceil(23.98) ceil(-2.3)

57.00 24.00 -2.00

double double

cmath floor(x) Returns the value rounded down to the next lower integer

floor(56.34) floor(45.67) floor(-2.3)

56.00 45.00 -3

double double

cmath cos(x) Returns the cosine of angle x

cos(0.0) 1.0 double double

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’)) static_cast<char>(tolower(‘a’))

a a

int int

cctype toupper(x) Returns the upper case of x

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

A A

int int

Page 2: Chapter 6 - Handout

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; // Define the function header // Return-type functionName (parameters); int abs( int x ) ; int main( ) { int a=3, b=-4, c; //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; }

Output 3 4 32 77

Page 3: Chapter 6 - Handout

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

double Max( double a, double b ) { if (a>b) return a; 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;

Page 4: Chapter 6 - Handout

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

Page 5: Chapter 6 - Handout

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 }