Sie sind auf Seite 1von 5

# Chapter 6: User Defined Functions I

Predefined Functions
Header cstdlib Function abs(x) Purpose Returns the absolute value of x Returns the absolute value of x Returns the value rounded up to the next higher integer Example Statement Result abs(-7) 7 abs(7) 7 fabs(-7.76) fabs(7.67) ceil(56.34) ceil(23.98) ceil(-2.3) floor(56.34) floor(45.67) floor(-2.3) cos(0.0) exp(1.0) pow(0.16, 0.5) static_cast<char> (tolower(A)) static_cast<char> (tolower(a)) 7.76 7.67 57.00 24.00 -2.00 56.00 45.00 -3 1.0 2.718 0.4 a a Parameters Type int Result Type int

cmath

fabs(x)

double

double

cmath

ceil(x)

double

double

cmath

floor(x)

double

double

## cos(x) exp(x) pow(x,y) tolower(x)

Returns the cosine of angle x Returns e to power x, where e = 2.718 Returns x to the power of y Returns the lower case of x

cctype

toupper(x)

int A A

int

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

## Enter three double values: 12 34 56 The largest value is: 56

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

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 }